-1

Quick question, how do you make the values in a list unique? I need to pass a list of numbers into a for loop that multiplies the number to 10 powered to its index and create a list of the answers. I was able to make the program to return the list, however, my code only seems to work for unique numbers and if there are any duplicates it assigns them with the same 10 powered to index.

def expanded_form(num):
    y = str(num)
    df = list(y)
    df.reverse()
    print(df)
    x = []
    product = []
    for n in df:
        o = pow(10, df.index(n))
        x.append(o)
    for n1, n2 in zip(x, df):
        product.append(str(int(n1) * int(n2)))
    product.reverse()
    p = [i for i in product if i != '0']
    p = [int(i) for i in p]
    p.sort(reverse=True)
    p = [str(i) for i in p]
    print(' + ' .join(p))

when I run this code for a number with repeating numbers like 4982342 it attaches the same values to repeating numbers. here it considers both 4s as 40 and both 2s as 2. how do I get it to consider them separately?

1
  • Welcome to StackOverflow, please try to write more concise questions, you can review this page how to ask. To comment on unique values in a list you can use list(set(your_list)) Commented Dec 6, 2020 at 19:03

3 Answers 3

0

I know this a bit terse, but it works for all cases in your description. I could expand it out if you would like it to be easier to read.

def expanded_form(num):
    print(" + ".join(map(str, filter(None, [int(v)*10**(len(num) - 1 - i) for i, v in enumerate(num)]))))

And to test it

test_numbers = [
        '123456789',
        '111111111',
        '111222211',
        '101010101',
]

for num in test_numbers:
    print("{} => {}".format(num, expanded_form(num)))
}

which produces:

123456789 => 100000000 + 20000000 + 3000000 + 400000 + 50000 + 6000 + 700 + 80 + 9
111111111 => 100000000 + 10000000 + 1000000 + 100000 + 10000 + 1000 + 100 + 10 + 1
111222211 => 100000000 + 10000000 + 1000000 + 200000 + 20000 + 2000 + 200 + 10 + 1
101010101 => 100000000 + 1000000 + 10000 + 100 + 1

Here is another version that may be a little easier to follow:

def expanded_form4(num):
    # Turn the string into a list of 1 character strings, and reverse that
    rev_num = reversed(list(num))
    # convert each element to an int
    rev_num_ints = map(int, rev_num)
    # multiply each element by 10**index of the number
    rev_num_ints_pow = [value*pow(10, index) for index, value in enumerate(rev_num_ints)]
    # remove 0 values.  `if value` evaluates to False for 0
    rev_num_ints_pow = [value for value in rev_num_ints_pow if value]
    # reverse again to get back to original order
    rev_num_ints_pow_original_order = reversed(rev_num_ints_pow)
    # convert the values back to strings
    rev_num_final = map(str, rev_num_ints_pow_original_order)
    # print it
    print(" + ".join(rev_num_final))
1
  • cool!! I'm a lil new to this so thank you soo much for explaining! (len(num) - 1 - I) can you please explain the logic behind this line? thank you for your patience Commented Dec 6, 2020 at 8:28
0
def expanded_form(num):
    y = str(num)
    df = list(y)
    df.reverse()
    print(df)
    x = []
    product = []
    for i,j in enumerate(df):
        o = pow(10, i)
        x.append(o)
    for n1, n2 in zip(x, df):
        product.append(str(int(n1) * int(n2)))
    product.reverse()
    p = [i for i in product if i != '0']
    p = [int(i) for i in p]
    p.sort(reverse=True)
    p = [str(i) for i in p]
    print(' + ' .join(p))

Use enumerate list to get index and value , so I stead of finding index of each value pass that index

Output:

enter image description here

1
  • @prarthikkrishnan please click the tick sign near to my answer to accept it :)
    – PDHide
    Commented Dec 6, 2020 at 7:48
0

Here is a simpler method to achieve the same:

 def expanded_form(num):
    lst = list(map(int, list(num)))[::-1]
    print(lst)
    lst = [lst[x]*10**x for x in range(len(lst))][::-1]
    lst = list(map(str, lst))
    print('+'.join(lst))

expanded_form('1233')

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.