Reconstruct an array by replacing every element with a i-1 K - Python

0 votes
I've recently started learning python and I've got an assignment to complete. One of the questions was to reconstruct an array by replacing every element with a[[i-1]%K]. Can somebody help me with this? Thanks you
Jun 14, 2019 in Python by Varsha

edited Jun 17, 2019 by Kalgi 1,425 views

1 answer to this question.

0 votes

Hey varsha, Have a look at this one:

def construct(s, k, a):
    index = 0
    # Finding the index which is not -1
    for i in range(s):
        if (a[i]!=-1):
            index = i
            break
    # Calculating the values of the indexes index-1 to 0
    for i in range(index-1, -1, -1):
        if (a[i]==-1):
            a[i]=(a[i + 1]-1 + k)% k
    # Calculating the values of the indexes index + 1 to n
    for i in range(index + 1, s):
        if(a[i]==-1):
            a[i]=(a[i-1]+1)% k
            print(a)
# Driver code
s, k = 6, 7
a =[1, 2, 3, 4, 5, 6]
construct(s, k, a)

It should give you the following output:

answered Jun 14, 2019 by Miya

Related Questions In Python

+1 vote
4 answers

How do I remove an element from a list by index in Python?

Delete the List and its element: We have ...READ MORE

answered Jun 7, 2020 in Python by sahil
• 500 points
280,922 views
0 votes
1 answer

I want to download a file from the website by web scraping. Can anyone explain how to do this in jupyter lab (python) with an example?

Hey, Web scraping is a technique to automatically ...READ MORE

answered Apr 7, 2020 in Python by Gitika
• 65,770 points
2,425 views
0 votes
1 answer

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

Hello @kartik, Since different users might have different ...READ MORE

answered Jun 15, 2020 in Python by Niroj
• 82,840 points
23,384 views
0 votes
0 answers

The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

I couldn't figure out the problem. here my ...READ MORE

Dec 17, 2020 in Python by muammer
• 120 points
7,790 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,468 views
0 votes
1 answer
+5 votes
6 answers

Lowercase in Python

You can simply the built-in function in ...READ MORE

answered Apr 11, 2018 in Python by hemant
• 5,790 points
4,128 views
0 votes
1 answer

how do I check the length of an array in a python program?

lets say we have a list mylist = ...READ MORE

answered Mar 12, 2019 in Python by Mohammad
• 3,230 points
1,231 views
0 votes
1 answer

In Python, how do I read a file line-by-line into a list?

with open(fname) as f:     content = f.readlines() # you ...READ MORE

answered Oct 9, 2018 in Python by SDeb
• 13,300 points
1,822 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP