Александра обнови решението на 03.11.2022 17:45 (преди над 2 години)
+"""Homework 02 module for fmi-intro-to-python-2022"""
+
+# Representation of a phone keyboard
+key_phone_dict = {
+ '0' : (' '),
+ '2' : ('A', 'B', 'C'),
+ '3' : ('D', 'E', 'F'),
+ '4' : ('G', 'H', 'I'),
+ '5' : ('J', 'K', 'L'),
+ '6' : ('M', 'N', 'O'),
+ '7' : ('P', 'Q', 'R', 'S'),
+ '8' : ('T', 'U', 'V'),
+ '9' : ('W', 'X', 'Y', 'Z')
+}
+
+def nums_to_text(nums):
+ '''Translate a list of numbers from a phone keypad into text.'''
Докстринговете трябва да се ограждат в "
, а не с '
. #PEP8
Не държа на това, но съм длъжен да споделя.
+ # Split the input list into sublists, containing only the consecutively
+ # occurring identical characters
+ indexes = []
+ for index, num in enumerate(nums):
+ if num != nums[index-1] or len(nums) == 1:
Моля слагай итнервал около -
и други аритметични оператори.
+ indexes.append(index)
+ indexes.append(len(nums))
+ separated_list = []
+ for i, index in enumerate(indexes):
+ if i != len(indexes) - 1:
+ sliced_list = nums[index:indexes[i+1]]
+ separated_list.append(sliced_list)
+ # Return a string with the translated digits, taken from the corresponding
+ # dictionary pair
+ output = ''
+ for num_element in separated_list:
+ letter = num_element[0]
+ current_iter = key_phone_dict.get(str(letter))
+ if letter in [-1, 1]:
+ continue
+ else:
+ output += current_iter[(len(num_element)%(len(current_iter)))-1]
+ return output
+
+def text_to_nums(text):
+ '''Translate a text from a phone keypad into combination of numbers.'''
+ # Make the input case-insensitive
+ text_lst = list(text.upper())
+ # Search for the letter in the dictionary values and translate it using
+ # the corresponding key
+ output_list = []
+ for i, letter in enumerate(text_lst):
+ for number, letters in key_phone_dict.items():
+ if letter in letters:
+ num_times = letters.index(letter) + 1
+ output_list.extend(int(number) for i in range(num_times))
Не е кой знае какво, но според мен е по-четимо. Пък и дори да не се съгласиш - може да не знаеш, че можеш да умножаваш list
output_list.extend([int(number)] * num_times)
+ # Check for consecutive duplicating characters
+ if i+1 != len(text_lst) and text_lst[i+1] == letter:
+ output_list.append(-1)
+ return output_list
+
+def nums_to_angle(nums):
+ '''
+ Take a list of numbers and return a normalized angle, relative to
+ the rotary dial phone idea.
+ '''
+ angle = 0
+ sum_of_angles = 0
+ for num in nums:
+ if num == 0:
+ angle = 300
+ else:
+ angle = num*30
+ sum_of_angles += angle
+ return sum_of_angles%360
+
+def angles_to_nums(angles):
+ '''
+ Take a list of angles and return a list of numbers corresponding to them,
+ relative to the the rotary dial phone idea.
+ '''
+ nums = []
+ for angle in angles:
+ angle = angle%360
+ num = angle//30
+ rem = angle%30
+ if rem > 15:
+ num += 1
+ if num == 0 or num > 10:
+ continue
+ if num == 10:
+ num = 0
+ nums.append(num)
+ return nums
+
+def is_phone_tastic(word):
+ '''Return true/false depending on whether the given word is phone-tastic.'''
+ # Text-to-nums conversion
Не ми харесват имената на променливите. Защо не nums
вместо ttn_list
и angle
вместо n_angle
?
+ ttn_list = text_to_nums(word)
+ # Normalized angle
+ n_angle = nums_to_angle(ttn_list)
+ return (n_angle%len(word) == 0)
Моля не слагай скоби при return
, освен ако разбира се това не е нужно, но тук не е
Докстринговете трябва да се ограждат в
"
, а не с'
. #PEP8Не държа на това, но съм длъжен да споделя.
Моля слагай итнервал около
-
и други аритметични оператори.Не е кой знае какво, но според мен е по-четимо. Пък и дори да не се съгласиш - може да не знаеш, че можеш да умножаваш
list
output_list.extend([int(number)] * num_times)
Не ми харесват имената на променливите. Защо не
nums
вместоttn_list
иangle
вместоn_angle
?Моля не слагай скоби при
return
, освен ако разбира се това не е нужно, но тук не е