Hướng dẫn weight of string in python - trọng lượng của chuỗi trong python

Cải thiện bài viết

Lưu bài viết

  • Đọc
  • Bàn luận
  • Cải thiện bài viết

    Lưu bài viết

    Đọc

    Input :  test_str = 'GeeksforGeeks', 
             {"G" : 1, "e" : 2, "k" : 5, "f" : 3, "s" : 15, "o" : 4, "r" : 6} 
    Output : 63 
    Explanation : 2 [G*2] + 8[e*4] + 30[s*2] + 10[k*2] + 4[o] + 6[r] +3[f] = 63.
    Input : test_str = 'Geeks', {"G" : 1, "e" : 2, "k" : 5, "s" : 15} 
    Output : 25  

    Bàn luậnUsing loop

    Cho một chuỗi, mỗi ký tự được ánh xạ với trọng lượng [số], tính tổng trọng lượng của chuỗi.

    Python3

    Phương pháp số 1: Sử dụng vòng lặp

    Đây là một trong những cách mà nhiệm vụ này có thể được thực hiện. Trong đó, chúng tôi lặp lại cho tất cả các ký tự và tổng hợp tất cả các trọng số được ánh xạ từ từ điển.

    Các

    The original string is : GeeksforGeeks
    The weighted sum : 81
    8
    The original string is : GeeksforGeeks
    The weighted sum : 81
    9
    The original string is : GeeksforGeeks
    The weighted sum : 81
    0
    The original string is : GeeksforGeeks
    The weighted sum : 81
    1
    The original string is : GeeksforGeeks
    The weighted sum : 81
    2
    The original string is : GeeksforGeeks
    The weighted sum : 81
    3

    The original string is : GeeksforGeeks
    The weighted sum : 81
    4=
    Input : test_str = 'Geeks', {"G" : 1, "e" : 2, "k" : 5, "s" : 15} 
    Output : 25  
    2

    The original string is : GeeksforGeeks
    The weighted sum : 81
    7=
    The original string is : GeeksforGeeks
    The weighted sum : 81
    9
    The original string is : GeeksforGeeks
    The weighted sum : 81
    0
    The original string is : GeeksforGeeks
    The weighted sum : 81
    1
    The original string is : GeeksforGeeks
    The weighted sum : 81
    2
    The original string is : GeeksforGeeks
    The weighted sum : 81
    3

    The original string is : GeeksforGeeks
    The weighted sum : 81
    4
    The original string is : GeeksforGeeks
    The weighted sum : 81
    5
    The original string is : GeeksforGeeks
    The weighted sum : 81
    6

    The original string is : GeeksforGeeks
    The weighted sum : 81

    The original string is : GeeksforGeeks
    The weighted sum : 81
    8
    The original string is : GeeksforGeeks
    The weighted sum : 81
    9
    The original string is : GeeksforGeeks
    The weighted sum : 81
    9
    The original string is : GeeksforGeeks
    The weighted sum : 81
    1
    The original string is : GeeksforGeeks
    The weighted sum : 81
    2test_dict 2
    O[n]

    Đầu raO[n]

    Độ phức tạp về thời gian: O [n]Using sum[]

    Không gian phụ trợ: O [n]

    Python3

    Phương pháp số 1: Sử dụng vòng lặp

    Đây là một trong những cách mà nhiệm vụ này có thể được thực hiện. Trong đó, chúng tôi lặp lại cho tất cả các ký tự và tổng hợp tất cả các trọng số được ánh xạ từ từ điển.

    Các

    The original string is : GeeksforGeeks
    The weighted sum : 81
    8
    The original string is : GeeksforGeeks
    The weighted sum : 81
    9
    The original string is : GeeksforGeeks
    The weighted sum : 81
    0
    The original string is : GeeksforGeeks
    The weighted sum : 81
    1
    The original string is : GeeksforGeeks
    The weighted sum : 81
    2
    The original string is : GeeksforGeeks
    The weighted sum : 81
    3

    : 5: 6

    The original string is : GeeksforGeeks
    The weighted sum : 81
    7=
    The original string is : GeeksforGeeks
    The weighted sum : 81
    9
    The original string is : GeeksforGeeks
    The weighted sum : 81
    0
    The original string is : GeeksforGeeks
    The weighted sum : 81
    1
    The original string is : GeeksforGeeks
    The weighted sum : 81
    2
    The original string is : GeeksforGeeks
    The weighted sum : 81
    3

    The original string is : GeeksforGeeks
    The weighted sum : 81
    4
    The original string is : GeeksforGeeks
    The weighted sum : 81
    5
    The original string is : GeeksforGeeks
    The weighted sum : 81
    6

    The original string is : GeeksforGeeks
    The weighted sum : 81

    The original string is : GeeksforGeeks
    The weighted sum : 81
    8
    The original string is : GeeksforGeeks
    The weighted sum : 81
    9
    The original string is : GeeksforGeeks
    The weighted sum : 81
    9
    The original string is : GeeksforGeeks
    The weighted sum : 81
    1
    The original string is : GeeksforGeeks
    The weighted sum : 81
    2test_dict 2
    O[n]

    Đầu raO[n]

    Độ phức tạp về thời gian: O [n]Using list[],set[],count[] methods

    Python3

    Không gian phụ trợ: O [n]

    Phương pháp số 2: Sử dụng Sum []

    Đây là một cách nữa trong đó nhiệm vụ này có thể được thực hiện. Trong đó, chúng tôi sử dụng biểu thức máy phát và sum [] được sử dụng để tính toán tổng trọng lượng riêng lẻ.

    The original string is : GeeksforGeeks
    The weighted sum : 81
    7= 'tough'7
    The original string is : GeeksforGeeks
    The weighted sum : 81
    9'tough'9
    The original string is : GeeksforGeeks
    The weighted sum : 81
    9: 1 : 2: 3: 4

    Phương thức số 3: Sử dụng Danh sách [], Set [], Count [] Phương thức

    Input : test_str = 'Geeks', {"G" : 1, "e" : 2, "k" : 5, "s" : 15} 
    Output : 25  
    47=
    Input : test_str = 'Geeks', {"G" : 1, "e" : 2, "k" : 5, "s" : 15} 
    Output : 25  
    49
    The original string is : GeeksforGeeks
    The weighted sum : 81
    9
    Input : test_str = 'Geeks', {"G" : 1, "e" : 2, "k" : 5, "s" : 15} 
    Output : 25  
    51
    Input : test_str = 'Geeks', {"G" : 1, "e" : 2, "k" : 5, "s" : 15} 
    Output : 25  
    11

    Input : test_str = 'Geeks', {"G" : 1, "e" : 2, "k" : 5, "s" : 15} 
    Output : 25  
    03=
    Input : test_str = 'Geeks', {"G" : 1, "e" : 2, "k" : 5, "s" : 15} 
    Output : 25  
    05

    Input : test_str = 'Geeks', {"G" : 1, "e" : 2, "k" : 5, "s" : 15} 
    Output : 25  
    57
    Input : test_str = 'Geeks', {"G" : 1, "e" : 2, "k" : 5, "s" : 15} 
    Output : 25  
    58
    The original string is : GeeksforGeeks
    The weighted sum : 81
    1=
    Input : test_str = 'Geeks', {"G" : 1, "e" : 2, "k" : 5, "s" : 15} 
    Output : 25  
    61
    Input : test_str = 'Geeks', {"G" : 1, "e" : 2, "k" : 5, "s" : 15} 
    Output : 25  
    62
    Input : test_str = 'Geeks', {"G" : 1, "e" : 2, "k" : 5, "s" : 15} 
    Output : 25  
    63

    The original string is : GeeksforGeeks
    The weighted sum : 81
    8
    The original string is : GeeksforGeeks
    The weighted sum : 81
    9
    Input : test_str = 'Geeks', {"G" : 1, "e" : 2, "k" : 5, "s" : 15} 
    Output : 25  
    08
    The original string is : GeeksforGeeks
    The weighted sum : 81
    1
    The original string is : GeeksforGeeks
    The weighted sum : 81
    2
    Input : test_str = 'Geeks', {"G" : 1, "e" : 2, "k" : 5, "s" : 15} 
    Output : 25  
    11

    The original string is : GeeksforGeeks
    The weighted sum : 81
    4
    The original string is : GeeksforGeeks
    The weighted sum : 81
    5
    The original string is : GeeksforGeeks
    The weighted sum : 81
    6

    The original string is : GeeksforGeeks
    The weighted sum : 81


    Trọng lượng của chuỗi là bao nhiêu?

    Trọng lượng của chuỗi được tính bằng cách thêm các giá trị trọng lượng của từng ký tự trong chuỗi đó. Giá trị trọng lượng của mỗi ký tự có trong chuỗi chỉ sau ký tự. Trọng lượng của mỗi ký tự sẽ là một số chữ số [từ 0 đến 9].calculated by adding the the weight values of each characters in that string. The weight value of each character is present in the string only just after the character. The weight of each character will be a single digit number[from 0 to 9].

    Trọng lượng trong Python là bao nhiêu?

    Trọng lượng là một tham số tùy chọn được sử dụng để cân nhắc khả năng cho từng giá trị.3. Cum_weights là một tham số tùy chọn được sử dụng để cân nhắc khả năng cho từng giá trị nhưng trong khả năng này được tích lũy.4. K là một tham số tùy chọn được sử dụng để xác định độ dài của danh sách được trả về.an optional parameter which is used to weigh the possibility for each value. 3. cum_weights is an optional parameter which is used to weigh the possibility for each value but in this the possibility is accumulated. 4. k is an optional parameter that is used to define the length of the returned list.

    Kích thước của chuỗi trong Python là gì?

    Để tính độ dài của một chuỗi trong Python, bạn có thể sử dụng phương thức Len [] tích hợp.Nó lấy một chuỗi làm tham số và trả về một số nguyên làm độ dài của chuỗi đó.Ví dụ, Len [giáo dục của người Hồi giáo] sẽ trở lại 9 vì có 9 ký tự trong giáo dục giáo dục.use the built-in len[] method. It takes a string as a parameter and returns an integer as the length of that string. For example, len[“educative”] will return 9 because there are 9 characters in “educative”.

    Chuỗi [] có nghĩa là gì trong Python?

    Giống như nhiều ngôn ngữ lập trình phổ biến khác, các chuỗi trong Python là các mảng byte đại diện cho các ký tự Unicode.Tuy nhiên, Python không có kiểu dữ liệu ký tự, một ký tự duy nhất chỉ đơn giản là một chuỗi có chiều dài 1. Giá đỡ vuông có thể được sử dụng để truy cập các phần tử của chuỗi.arrays of bytes representing unicode characters. However, Python does not have a character data type, a single character is simply a string with a length of 1. Square brackets can be used to access elements of the string.

    Bài Viết Liên Quan

    Chủ Đề