简体   繁体   中英

Replace integers in a list with a string Python

I am working on a coding assignment in Python and I am struggling to understand why my code is not working.

Write a function in Python called "BooHoo" which takes in an integer, n, and stores all integers from 1 to n in a list. However, if an integer in the list is divisible by 3, the list should instead contain "Boo". If the integer is divisible by 5 the list should instead contain "Hoo". If the integer is divisible by both 3 and 5, the list should instead contain "BooHoo".

def BooHoo(n):
'''
A function which returns a list of integers, and replaces integers divisible by 3 with "Boo" and integers
divisible by 5 with "Hoo" and integers divisible by 3 and 5 with "BooHoo"


Parameters
-------
n: an integer


Returns
--------

final_list: a Python list

'''

main_list = []
for x in range(n):
    main_list.append(x)


for idx, j in enumerate(main_list):
    if not (idx % 3) and not (idx % 5):
        main_list.pop(idx)
        main_list.insert(idx, 'BooHoo')
    elif not (idx % 3):
            main_list.pop(idx)
            main_list.insert(idx, 'Boo')
    elif not (idx % 5):
            main_list.pop(idx)
            main_list.insert(idx, 'Hoo')
    else:
        continue

final_list = [main_list]

return final_list

There were some logical errors regarding the index and actual elements of the list. I have highlighted the modified/added lines by a comment # . Mainly, you needed to replace idx by j because idx was an index but j was an actual element. If you start by range(n) , it doesn't matter because the index is the same as j . But since you mentioned in your question, you want to store numbers from 1 up to n , you need to use range(1, n+1)

def BooHoo(n):
    main_list = []
    for x in range(1,n+1): # replaced range(n) to start from 1
        main_list.append(x)

    for idx, j in enumerate(main_list):
        if not (j % 3) and not (j % 5): # replaced idx by j
            main_list.pop(idx)
            main_list.insert(idx, 'BooHoo')
        elif not (j % 3): # replaced idx by j
                main_list.pop(idx)
                main_list.insert(idx, 'Boo')
        elif not (j % 5): # replaced idx by j
                main_list.pop(idx)
                main_list.insert(idx, 'Hoo')
        else:
            continue

    return main_list # Removed unnecessary second list

# Call the function
print (BooHoo(15))

Output

[1, 2, 'Boo', 4, 'Hoo', 'Boo', 7, 8, 'Boo', 'Hoo', 11, 'Boo', 13, 14, 'BooHoo']

A better way to solve your problem is to construct the right list from the start (with a loop or list comprehension) than to modify a sequential list of numbers.

def BooHoo(n):
    return ['BooHoo' if not (i % 5 or i % 3) else 
               'Hoo' if not i % 5 else 
               'Boo' if not i % 3 else 
                   i for i in range(1,n+1)]

And, more for the sake of fun than usefulness, a dictionary-based solution:

def BooHoo(n):
    words = {(1,1): 'BooHoo', (1,0): 'Boo', (0,1): 'Hoo'}
    return [words.get((i % 3 == 0, i % 5 == 0), i) for i in range(1, n+1)]

Why go through all that trouble of appending and popping so much, thats not necessary

def BooHoo(n):

    for index, item in enumerate(list(range(1, (n+1))):
        if not item % 5 and not item % 3:
            lista[index] = "BooHoo"
        elif not item % 3:
            lista[index] = "Boo"
        elif not item % 5:
            lista[index] = "Hoo"
        else:
            pass

    return lista 

print(BooHoo(25))

Output

(xenial)vash@localhost:~/python/stack_overflow$ python3.7 boohoo.py [1, 2, 'Boo', 4, 'Hoo', 'Boo', 7, 8, 'Boo', 'Hoo', 11, 'Boo', 13, 14, 'BooHoo', 16, 17, 'Boo', 19, 'Hoo', 'Boo', 22, 23, 'Boo', 'Hoo']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM