There is a task, completed it partially and stuck. I can not figure out how to organize the implementation of the last 3rd paragraph. I would be very grateful if you help me figure it out.

Task: Write the counter (T) function:

  1. host tuple consisting of Latin alphabet strings, for example, ("ABC", "abc")
  2. leading lines to a single (upper or lower) case defining the number of unique Latin characters for each line
  3. (the string "Aaa" contains only 1 unique character) returns the length of the line with the maximum number of unique characters (if there are several such lines, then the largest of them)

Program Code:

def counter(T): # Ρ‡ΠΈΡ‚Π°Π΅ΠΌ строку ΠΈ ΠΏΡ€ΠΈΠ²ΠΎΠ΄ΠΈΠΌ ΠΊ Π½ΠΈΠΆΠ½Π΅ΠΌΡƒ рСгистру, Ρ€Π°Π·Π±ΠΈΠ²Π°Π΅ΠΌ Π½Π° строки T=str(T) T=T[2:-2] T=T.lower().split("\', \'") length_unic=[] length_str=[] length_max = 0 # измСняСм число ΡƒΠ½ΠΈΠΊΠ°Π»ΡŒΠ½Ρ‹Ρ… символов ΠΈ Π΄Π»ΠΈΠ½Ρƒ строк, записываСм Π² список for word in T: length_current = len(word) if length_current > length_max: length_max = length_current length_str.append(length_current) unic = set() for letter in word: unic.add(letter) length_unic.append(len(unic)) # поиск максимального ΡƒΠ½ΠΈΠΊΠ°Π»ΡŒΠ½ΠΎΠ³ΠΎ max=0 for i in range(len(length_unic)): if length_unic[i] > max: max = length_unic[i] # ΠΈΠ·ΠΌΠ΅Ρ€Π΅Π½ΠΈΠ΅ Π΄Π»ΠΈΠ½Ρ‹ ΠΊΠ°ΠΆΠ΄ΠΎΠΉ строки max_len_unic = length_unic[0] for i in range(1,len(T)): if length_unic[i] >= max_len_unic: max_len_unic = length_unic[i] max_len_str = length_str[0] if length_str[i] >= max_len_str: max_len_str = length_unic[i] #print(length_max) print(length_unic) print(length_str) T=('Aa', 'ab', 'AaAa', 'AaAaAa', 'ABBA') counter(T) 

    3 answers 3

    I'm not sure that I understood the task correctly until the end, but the solution should be:

     def counter(T): # Ρ‡ΠΈΡ‚Π°Π΅ΠΌ строку ΠΈ ΠΏΡ€ΠΈΠ²ΠΎΠ΄ΠΈΠΌ ΠΊ Π½ΠΈΠΆΠ½Π΅ΠΌΡƒ рСгистру, Ρ€Π°Π·Π±ΠΈΠ²Π°Π΅ΠΌ Π½Π° строки T=str(T) T=T[2:-2] T=T.lower().split("\', \'") length_unic=[] length_str=[] length_max = 0 # измСняСм число ΡƒΠ½ΠΈΠΊΠ°Π»ΡŒΠ½Ρ‹Ρ… символов ΠΈ Π΄Π»ΠΈΠ½Ρƒ строк, записываСм Π² список for word in T: length_current = len(word) if length_current > length_max: length_max = length_current length_str.append(length_current) unic = set() for letter in word: unic.add(letter) length_unic.append(len(unic)) # поиск максимального ΡƒΠ½ΠΈΠΊΠ°Π»ΡŒΠ½ΠΎΠ³ΠΎ max_uniq=0 for i in range(len(length_unic)): if length_unic[i] > max_uniq: max_uniq = length_unic[i] # ΠΈΠ·ΠΌΠ΅Ρ€Π΅Π½ΠΈΠ΅ Π΄Π»ΠΈΠ½Ρ‹ ΠΊΠ°ΠΆΠ΄ΠΎΠΉ строки max_len_unic = length_unic[0] for i in range(1,len(T)): if length_unic[i] >= max_len_unic: max_len_unic = length_unic[i] max_len_str = length_str[0] if length_str[i] >= max_len_str: max_len_str = length_unic[i] #print(length_max) print(length_unic) print(length_str) tmp_max = max(length_unic) tmp_res = [[index, len(T[index])] for index, value in enumerate(length_unic) if value == tmp_max] print(max(tmp_res, key=lambda x:x[1])[1]) T=('Aa', 'ab', 'AaAa', 'AaAaAa', 'ABBAbb') counter(T) 

    And yet - you have created the max variable, although this is an internal function that finds the maximum in the array. I re-named it as max_uniq to make everything work correctly.

    • Thanks for the help, everything worked out! - Alex Sapsay

    According to the question, the problem is solved approximately as follows:

     def counter(T): # это Π½Π΅Ρ‡Ρ‚ΠΎ Π²Ρ€ΠΎΠ΄Π΅ Π±Π°Π·ΠΎΠ²ΠΎΠΉ ΠΏΡ€ΠΎΠ²Π΅Ρ€ΠΊΠΈ ΠΏΠ΅Ρ€Π΅Π΄Π°Π²Π°Π΅ΠΌΠΎΠ³ΠΎ Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠΈ ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€Π° if 'tuple' != type(T).__name__ or \ 1 in list(map(lambda x: 0 in [c.isalpha() for c in x], T)): print('ΠŸΠ΅Ρ€Π΅Π΄Π°Π½Π½Ρ‹Π΅ Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠΈ Π΄Π°Π½Π½Ρ‹Π΅ ΠΈΠΌΠ΅ΡŽΡ‚ Π½Π΅Π²Π΅Ρ€Π½Ρ‹ΠΉ Ρ„ΠΎΡ€ΠΌΠ°Ρ‚.') return # Π½Π°Ρ…ΠΎΠ΄ΠΈΠΌ количСство ΡƒΠ½ΠΈΠΊΠ°Π»ΡŒΠ½Ρ‹Ρ… символов для ΠΊΠ°ΠΆΠ΄ΠΎΠΉ ΠΈΠ· строк ΠΊΠΎΡ€Ρ‚Π΅ΠΆΠ° print(list(map(lambda x: len(set(x.lower())), T))) # Π²Ρ‹Π²ΠΎΠ΄ΠΈΠΌ Π΄Π»ΠΈΠ½Ρƒ строки с ΠΌΠ°ΠΊΡΠΈΠΌΠ°Π»ΡŒΠ½Ρ‹ΠΌ количСством ΡƒΠ½ΠΈΠΊΠ°Π»ΡŒΠ½Ρ‹Ρ… символов tmp = list(map(lambda x: len(set(x)), T)) print(len(T[tmp.index(max(tmp))])) 
    • Thanks for the tip, helped to figure it out. - Alex Sapsay

    Another solution with the computational complexity O(n) (in one pass):

     def counter(T): d = {} max_uniq = 0 max_len = 0 for i,s in enumerate(T): d[s] = {'len': len(s), 'uniq_syms':len(set(s.lower()))} if d[s]['uniq_syms'] == max_uniq: if d[s]['len'] > max_len: max_len = d[s]['len'] elif d[s]['uniq_syms'] > max_uniq: max_uniq = d[s]['uniq_syms'] max_len = d[s]['len'] return d, max_len d, max_len = counter(T) 

    result:

     In [27]: max_len Out[27]: 4 In [28]: d Out[28]: {'Aa': {'len': 2, 'uniq_syms': 1}, 'ab': {'len': 2, 'uniq_syms': 2}, 'AaAa': {'len': 4, 'uniq_syms': 1}, 'AaAaAa': {'len': 6, 'uniq_syms': 1}, 'ABBA': {'len': 4, 'uniq_syms': 2}} 
    • Beautiful solution, algorithmically optimal) thanks for the help - Alex Sapsay