Monday, May 31, 2021

Re: how to input list [] dynamic in python

This does look a programming exercise in a class...  if you ask these questions in future, be warned people here will probably ask to see your attempt at writing code first (otherwise you are not really learning to be a programmer).

But, OK, it seemed interesting to me,so here is one approach:

def elementswise_left_join(l1, l2):
    """https://www.w3resource.com/python-exercises/list/python-data-type-list-exercise-155.php"""
    f_len = len(l1) - (len(l2) - 1)
    for i in range(0, len(l2), 1):
        if f_len - i > len(l1):
            break
        else:
            l1[i] = l1[i] + l2[i]
    return l1
    

def get_array(n):
    if n == 1:
        return [1,]
    r = [x for x in range(1, n + 1)]
    return r


curr = []
valid = True
while valid:
    num = input("Enter number: ")
    if num:
        new = get_array(int(num))
        if len(new) > len(curr):
            curr = elementswise_left_join(new, curr)
        else:
            curr = elementswise_left_join(curr, new)
        print(curr)
    else:
        valid = False



On Monday, 31 May 2021 at 13:34:51 UTC+2 leoa...@gmail.com wrote:
how about if input like in below
1 ) input: 3 , return [1, 2, 3]
then continue dynamic

2) input: 2, return [2, 4, 3]
  then continue dynamic  

3) input: 6, return [3, 6, 6, 4, 5, 6]
  then continue dynamic  

4) input: 1, return [4, 6, 6, 4, 5, 6]
  then continue dynamic  

5) input: 1, return [5, 6, 6, 4, 5, 6]

thanks


Pada tanggal Sen, 31 Mei 2021 pukul 18.20 paidjoo indo <leoa...@gmail.com> menulis:
hello i having question, because i still learn python for me, so can help me 
the question is

how about if input like in below
1 ) input: 3 , return [1, 2, 3]

2) input: 2, return [2, 4, 3]

3) input: 6, return [3, 6, 6, 4, 5, 6]

4) input: 1, return [4, 6, 6, 4, 5, 6]

5) input: 1, return [5, 6, 6, 4, 5, 6]

thanks in advance

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/3a5ee884-4d66-4fc3-8217-64658d7f585dn%40googlegroups.com.

No comments:

Post a Comment