Advertisement
UniQuet0p1

Untitled

Oct 17th, 2020
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.62 KB | None | 0 0
  1. """T07."""
  2.  
  3.  
  4. def organise_by_first_symbol(strings: list) -> dict:
  5. """
  6. Return dict where key the is a symbol and the value is a list of words starting with this symbol.
  7.  
  8. organise_by_first_symbol(["hello", "word", "world", "welcome", "yes"]) =>
  9. {'h': ['hello'], 'w': ['word', 'world', 'welcome'], 'y': ['yes']}
  10.  
  11. :param strings: List of strings
  12. :return: Dict with starting symbol and corresponding words.
  13. """
  14. new = {}
  15. dic = []
  16. for i in strings:
  17. dic[:] = i
  18. if dic[0] in new:
  19. new[dic[0]].append(i)
  20. else:
  21. new[dic[0]] = [i]
  22. return new
  23.  
  24.  
  25. def count_symbol_appearances(stringy: str) -> dict:
  26. """
  27. Return dict where key is the symbol and the value is the count this symbol is present in the string.
  28.  
  29. count_symbol_appearances("hello hi") => {'h': 2, 'e': 1, 'l': 2, 'o': 1, ' ': 1, 'i': 1}
  30.  
  31. :param stringy: String to be processed.
  32. :return: Dict with symbol counts.
  33. """
  34. new = {}
  35. dic = []
  36. dic[:] = stringy
  37. for letter in dic:
  38. if letter not in new:
  39. new.setdefault(letter, int(1))
  40. else:
  41. new[letter] = new[letter] + int(1)
  42. return new
  43.  
  44.  
  45. def get_unique_values(dictionary: dict) -> list:
  46. """
  47. Return the list of unique dict values.
  48.  
  49. get_unique_values({1: "a", 2: "b"})) => ["a", "b"]
  50. get_unique_values({1: "a", 2: "b", 3: "a"})) => ["a", "b"]
  51.  
  52. The order is not important.
  53. :param dictionary: Dict to be processed.
  54. :return: List of unique values.
  55. """
  56. new = []
  57. for letter in dictionary:
  58. if dictionary[letter] in new:
  59. continue
  60. else:
  61. new.append(dictionary[letter])
  62. return new
  63.  
  64.  
  65. def count_symbol_appearances_2(stringy: str) -> dict:
  66. """
  67. Return dict where key is a count and value is a list of symbols which appear this many times in the input string.
  68.  
  69. count_symbol_appearances_2("hello") => {1: ['e', 'o', 'h'], 2: ['l']}
  70. count_symbol_appearances_2("abcaba") => {2: ['b'], 1: ['c'], 3: ['a']}
  71.  
  72. :param stringy: String to be processed
  73. :return: Dict with counts and list of symbols
  74. """
  75. new = {}
  76. dic1 = {}
  77. dic = []
  78. dic[:] = stringy
  79. for i in stringy:
  80. dic1[i] = stringy.count(i)
  81. for o in dic1:
  82. if dic1[o] in new:
  83. new[dic1[o]].append(o)
  84. else:
  85. new[dic1[o]] = [o]
  86. return new
  87.  
  88.  
  89. def debug_numbers_in_string(stringy: str) -> list:
  90. """вю."""
  91. numbers = []
  92. for i in stringy:
  93. if i.isdigit():
  94. numbers.append(int(i))
  95.  
  96. return numbers
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement