Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Place strings to proper bucket
#python 3.5. def add_words_to_bucket(words): d = {} for word in words: l = len(word) if l in d: d[l].append(word) else: i = 0 while l >= 0 and not l in d: if not i: d[l] = [word] else: d[l] = [] l = l - 1 i += 1 # print(d) # {1: ['a', 'r'], 2: ['go', 'py'], 3: ['git'], 4: ['java', 'ruby'], 5: ['paper']} return d def get_as_list(d): bucket = [d[i] for i in range(0, len(d))] return bucket words = ["a", "git", "go", "py", "java", "paper", "ruby", "r"] d = add_words_to_bucket(words) bucket = get_as_list(d) print(d) # {0: [], 1: ['a', 'r'], 2: ['go', 'py'], 3: ['git'], 4: ['java', 'ruby'], 5: ['paper']} print (bucket) # [[], ['a', 'r'], ['go', 'py'], ['git'], ['java', 'ruby'], ['paper']] words2 = ["a", "git", "go", "py", "", "java", "paper", "ruby", "r","TheIpMan", ""] d2 = add_words_to_bucket(words2) bucket2 = get_as_list(d2) print(d2) # {0: ['', ''], 1: ['a', 'r'], 2: ['go', 'py'], 3: ['git'], 4: ['java', 'ruby'], 5: ['paper'], 6: [], 7: [], 8: ['TheIpMan']} print(bucket2) # [['', ''], ['a', 'r'], ['go', 'py'], ['git'], ['java', 'ruby'], ['paper'], [], [], ['TheIpMan']]
run
|
edit
|
history
|
help
0
Подарки
Python Unit Test Case Sample
Game3
python_study_note_for loop@nested loop
inerse_matrix_gauss_jordan
Rene
Hello world
Operator overloading.py
Homework exercise
gj2