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
edited Jun 17, 2019 61 views

## 1 answer to this question.

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

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

You can use the pop() method to ...READ MORE

## How can I find out the index of an element from row and column in Python?

You probably want to use np.ravel_multi_index: [code] import numpy ...READ MORE

## How can I define a multidimensional array in python using ctype?

Here's one quick-and-dirty method: >>> A = ((ctypes.c_float ...READ MORE

## How do I generate some random numbers with a distribution using Python?

scipy.stats.rv_discrete is what you ned IMHO. You can supply ...READ MORE

+1 vote

## how can i count the items in a list?

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

## how do i use the enumerate function inside a list?

can you give an example using a ...READ MORE

## Lowercase in Python

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