QUESTION:
Write the python code which does the following:
For a given array of positive numbers perform the following action:
a) Create a new array and copy the number from the first array to
the second array without copying the repeating numbers. (Second
array elements should be unique)
b) While creating second array place additional -1 integer for every 3 numbers that’s been added to the array. (After copying 5, 6, 7 from first array second array contents should be 5, 6, 7, -1)
After creating second array print it to console in a way that every
row will have only 3 elements of the second array.
Restrictions:
You are not allowed to use while loops.
You are not allowed to use [ and ] character in you code except for initialization of first and second array at the beginning of your code.
Example:
Input:
[5, 3, 20, 7, 32, 5, 2, 4, 19, 5, 45, 1, 7, 3, 2, 9, 5, 7, 6, 27, 74 ]
Resulting Array:
[5, 3, 20, -1, 7, 32, 2, -1, 4, 19, 45, -1, 1, 9, 6, -1, 27, 74]
output:
5 3 20
7 32 2
4 19 45
1 9 6
27 74
MY WORK:
A=[5,3,20,7,32,5,2,4,19,5,45,1,7,3,2,9,5,7,6,27,74]
B=[]
counter=0
n=1
for i in A:
if i not in B:
B.append(i)
counter+=1
if counter==3*n:
B.append(-1)
n+=1
print(B)
That is what i am getting as an output:
[5, 3, 20, -1, 7, 32, 2, -1, 4, 19, 45, -1, 1, 9, 6, -1, 27, 74]
I don't know how to print output as what they want without using brackets [].
I hope you can help me somehow.
You are very close! You just need to add another for loop in order to print your elements:
If i == -1 you print a new line to seperate these, if not you print the contents of the list B separated by end=" " (space character):
for i in B:
if i == -1:
# prints a new line character
# print("\n") if you need a blank line between entries
print()
else:
# end specifies how your elements will be seperated
print(i, end=" ")
Using your input, this prints:
5 3 20
7 32 2
4 19 45
1 9 6
27 74
You can iterate over arrays (and any other iterable) in Python by value in a simple for loop, thus
for element in B:
if element == -1:
print #this will just print new line marker
else:
print element, #notice the comma - it will not add new line marker
will print elements of your B container in a row, with new lines on -1 elements.
Print semantics used here assumes Python 2.X. If you are using Python 3.X then you need to change them to appropriate statements which prints new line print() and the one which does not print(element, end=' ')
Related
The problem statement is:
Design and implement an algorithm that displays the elements of a list
by interleaving an element from the beginning and an element from the
end.
For example, input:
1 2 3 4 5 6 7 8
Output :
1 8 2 7 3 6 4 5
This is what I tried, but I don't know what happen with elements 7 and 8:
lista = [1, 2, 3, 4, 5, 6, 7, 8]
for i in range(len(lista)):
lista.insert(2*i-1,lista.pop())
print("The list after shift is : " + str(lista))
# output:
# The list after shift is : [1, 7, 2, 8, 3, 6, 4, 5]
The only error in you code, is that range(len(lista)) starts from 0, not from 1. By starting from zero, in the first iteration 2*i-1 will be 2*0-1 = -1, and hence lista.insert(-1,lista.pop()), which means inserting at the very end of the list (that is what index -1 means in python).
To fix your code, you just need to start the range from 1. Actually, you are iterating too much, you can have your range just from 1 to the half of your list, like this:
lista = [1, 2, 3, 4, 5, 6, 7, 8]
for i in range(1, len(lista)//2):
lista.insert(2*i-1,lista.pop())
print("The list after shift is : " + str(lista))
# output:
# The list after shift is : [1, 8, 2, 7, 3, 6, 4, 5]
When you become more familiarized with the language, you will see that this can be accomplished much more easily.
For example, you can use the python slice syntax to achieve your goal. You slice from beginning to half , and from end to half (step of -1), then zip then together and flat.
[i for z in zip(lista[:4],lista[:-5:-1]) for i in z]
# [1, 8, 2, 7, 3, 6, 4, 5]
Another option:
import math
lista = [1, 2, 3, 4, 5, 6, 7, 8]
ans = []
for i in range(math.floor(len(lista)/2)):
ans.append(lista[i])
ans.append(lista[-i-1])
if (len(lista) % 2) != 0:
ans.append(lista(math.ceil(len(lista)/2)))
print(ans)
Technically speaking, I'd say it's two off-by-one errors (or one off-by-one error, but from -1 to +1, you'll see what I mean in the second paragraph). The first one is that you're subtracting 1 when you shouldn't. In the case when i = 0 (remember that range(n) goes from 0 to n-1), the insert position is being evaluated as 2*0-1 = (2*0)-1 = 0-1= -1 (for insert() method, that's the last position of the original list, pushing what was there forward, so it'll be the penultimate position of the NEW list).
But, when you remove the -1, the output becomes 8 1 7 2 6 3 5 4, which is close to what you want, but not quite right. What's missing is that the elements inserted should be at positions 1, 3, 5, 7, and not 0, 2, 4, 6. So, you'll actually need to add 1.
So, the shortest change to fix your code is to change lista.insert(2*i-1,lista.pop()) to lista.insert(2*i+1,lista.pop()).
Notice: if you put a print inside for, you'll realize that, after changing half the elements, the output is already right. That's because when len(lista) is 8, and you do lista.insert(x, lista.pop()) where x is bigger than 8, basically you're removing the last element (pop) and adding it at the end, so, nothing changes. Hence, you could also change range(len(lista)) to range(len(lista)//2). Test if it'll work when len(lista) is odd
Write a program with the definition of a function named Array_Swap() that will accept an integer list & its size as arguments and the function will swap elements in such a way that the first element is swapped with the last element, the second element is swapped with the second last element and so on, only if anyone or both the elements are odd and display the result.
If initially, a list of seven elements is: [5, 16, 4, 7, 19, 8, 2], the contents of the list after the execution should be:
[2, 16, 19, 7, 4, 8, 5].
def Array_Swap(List,Size):
for i in range (Size//2):
List[i]=List[Size//2-i]
print(List)
L=[]
n=int(input("Enter number of elements"))
for i in range(n):
x=int(input("Enter element"))
L.append(x)
Array_Swap(L,len(L))
The size/length of the list is not relevant because it can be obtained by len(list). And even then it's not required to conditionally swap items in the list. I suggest that the Size parameter be removed, but considering it's an assignment, it can be given a default of None so that it can be ignored by the caller if desired.
The following algorithm zips the input list with its reverse to form pairs relative to their index from the front and end of the list respectively, i.e. the first and last items are paired, the second and second last are paired, etc. Once the items are paired it is simply a matter of iterating over the list and emitting the second number of the pair if either number is odd, or the first number if neither is odd - effectively swapping the pairs as required.
This is done in-place (that's what the List[:] does) with a list comprehension.
def ArraySwap(List, Size=None):
List[:] = [b if (a % 2 or b % 2) else a
for a, b in zip(List, reversed(List))]
print(List)
>>> l = [5, 16, 4, 7, 19, 8, 2]
>>> ArraySwap(l)
[2, 16, 19, 7, 4, 8, 5]
>>> l
[2, 16, 19, 7, 4, 8, 5]
>>> l = list(range(1,30))
>>> ArraySwap(l)
[29, 2, 27, 4, 25, 6, 23, 8, 21, 10, 19, 12, 17, 14, 15, 16, 13, 18, 11, 20, 9, 22, 7, 24, 5, 26, 3, 28, 1]
>>> ArraySwap([1])
[1]
>>> ArraySwap([])
[]
To swap two elements in the list, use the pattern a, b = b, a.
If i is the index of a list item, it's opposite/mirror element is -(i+1), or - i - 1.
so for the 0th element (first one), the mirror is -(0+1), = -1
using that as the indexer for the element, swap the two list elements IF
check that at least one of them is odd before swapping:
def Array_Swap(List,Size):
for i in range (Size // 2):
if List[i] % 2 == 1 or List[-(i+1)] % 2 == 1:
List[i], List[-(i+1)] = List[-(i+1)], List[i]
print(List)
L = [5, 16, 4, 7, 19, 8, 2] # use your input blocks as before, this is an example
Array_Swap(L,len(L))
Output: [2, 16, 19, 7, 4, 8, 5]
(And if L = [5, 16, 4, 7, 19, 8, 1, 2], output is [2, 1, 4, 19, 7, 8, 16, 5].)
Btw, you don't need to pass in the size of the list as a parameter.
You could do just: for i in range(len(List) // 2)
Another solution:
def Array_Swap(List, Size=None):
if Size is None:
Size = len(List)
for (i, j) in zip(range(Size // 2), range(Size - 1, Size // 2, -1)):
if List[i] % 2 or List[j] % 2:
List[i], List[j] = List[j], List[i]
print(List)
Alternatively:
Size parameter is redundant since python's list instance knows its own size
Use bitwise operatos & | >>... possibly cheaper than modulus % and divide / operations.
def Array_Swap(List):
for i in range(len(List) >> 1):
if (List[i] | List[-i-1]) & 1:
List[i], List[-i-1] = List[-i-1], List[i]
print(List)
The standard way to swap two variables in Python is:
a, b = b, a
In this case, you would do:
lst[i], lst[size - i - 1] = lst[size - i - 1], lst[i]
which swaps the ith element with the element that is at index size - i - 1 (i.e. the ith index from the end).
The other issue with your code is that it doesn't check whether either of the elements being swapped are odd, which you can resolve by adding the condition:
if lst[i] % 2 or lst[size - i - 1] % 2:
before doing the swap. This uses the modulo operator (%) to check the parity of the elements. Taking a number modulo 2 will return 1 if the number is odd. If either are odd (1 has a truth value of True), the condition would succeed and the swap will be performed.
Finally, your function was printing the list, rather than returning it. Its usually best to return a result and print the returned result.
The full working version, with the above three changes is as follows:
def list_swap(lst, size):
for i in range(size // 2):
if lst[i] % 2 or lst[size - i - 1] % 2:
lst[i], lst[size - i - 1] = lst[size - i - 1], lst[i]
return lst
l = []
n = int(input("Enter number of elements: "))
for _ in range(n):
x = int(input("Enter element: "))
l.append(x)
result = list_swap(l, len(l))
print(result)
Also note, I've changed all the variables to be lowercase, which is standard in Python.
With your shown example:
Enter number of elements: 7
Enter element: 5
Enter element: 16
Enter element: 4
Enter element: 7
Enter element: 19
Enter element: 8
Enter element: 2
[2, 16, 19, 7, 4, 8, 5]
So I am trying to reverse an array from a .txt file without using the reverse function. here is what I have.
numbers = read() #creates numbers array out of the txt file
numbersrev = numbers #blank array for reverse
numLength = len(numbers) #measures the length of the array
print(numbers)
print("Array length of numbers: ", numLength)
i = numLength
i = i-1 #since the array starts at 0 instead of 1
k = 0
for k in range(8):
numbersrev[k] = numbers[i]
print ("The ", i," element of numbers is the ", k," element of numbersrev")
i -= 1
k += 1
print(numbersrev)
This is what I get after debugging on vscode:
[2, 4, 9, 11, 8, 3, 2, 5, 10]
Array length of numbers: 9
The 8 element of numbers is the 0 element of numbersrev
The 7 element of numbers is the 1 element of numbersrev
The 6 element of numbers is the 2 element of numbersrev
The 5 element of numbers is the 3 element of numbersrev
The 4 element of numbers is the 4 element of numbersrev
The 3 element of numbers is the 5 element of numbersrev
The 2 element of numbers is the 6 element of numbersrev
The 1 element of numbers is the 7 element of numbersrev
[10, 5, 2, 3, 8, 3, 2, 5, 10]
The top array is the original and the bottom array is the supposed reversal array
I cannot for the life of me find out why it stops changing the numbersrev array halfway through. Anybody know what the cause could be?
Okay, a few things...
First for loops increment their variables automatically in python.
so:
for k in range(8):
...
i -= 1
k += 1
should be:
for k in range(8):
...
i -= 1
No need to manually increment k.
Next lists are NOT arrays.
Lists in python are very different from arrays in a language like C.
Lists are mutable, and are passed by reference by default.
so when you try to make an empty array:
numbersrev = numbers #blank array for reverse
you are actually referencing the same 'list' from both numbers AND numbersrev
What you should have done is numbersrev = []
Then in your for loop, simply append to numbersrev rather than assign.
for k in range(numLength):
numbersrev.append(numbers[i])
print ("The ", i," element of numbers is the ", k," element of numbersrev")
i -= 1
Lastly
you could/should reference the length of numbers rather than a hardcoded value in your for loop, but how you have it will still work (assuming you ONLY ever get 8 numbers)
for k in range(numLength):
...
All together
numbers = read() #creates numbers array out of the txt file
numbersrev = [] #blank array for reverse
numLength = len(numbers) #measures the length of the array
print(numbers)
print("Array length of numbers: ", numLength)
i = numLength
i = i-1 #since the array starts at 0 instead of 1
for k in range(numLength):
numbersrev.append(numbers[i])
print ("The ", i," element of numbers is the ", k," element of numbersrev")
i -= 1
print(numbersrev)
numbersrev = numbers sets numbersrev to point to the same list as numbers, meaning when you modify numbers or numbersrev you're modifying the other at the same time. To make an actual copy of the object, you instead need to call numbersrev = numbers.copy(). Also, #sahasrara62's comment is correct, you need to call for k in range(numLength) instead of for k in range(8)
What you've done here is assign the reversed array as the normal array with this line:
numbersrev = numbers #blank array for reverse
What you're actually doing with that loop is this:
numbers[0] = numbers[9] # [10, 2, 3, 4, 5, 6, 7, 8, 9, 10]
numbers[1] = numbers[8] # [10, 9, 3, 4, 5, 6, 7, 8, 9, 10]
numbers[2] = numbers[7] # [10, 9, 8, 4, 5, 6, 7, 8, 9, 10]
numbers[3] = numbers[6] # [10, 9, 8, 7, 5, 6, 7, 8, 9, 10]
numbers[4] = numbers[5] # [10, 9, 8, 7, 6, 6, 7, 8, 9, 10]
numbers[5] = numbers[4] # [10, 9, 8, 7, 6, 6, 7, 8, 9, 10]
numbers[6] = numbers[3] # [10, 9, 8, 7, 6, 6, 7, 8, 9, 10]
numbers[7] = numbers[2] # [10, 9, 8, 7, 6, 6, 7, 8, 9, 10]
numbers[8] = numbers[1] # [10, 9, 8, 7, 6, 6, 7, 8, 9, 10]
numbers[9] = numbers[0] # [10, 9, 8, 7, 6, 6, 7, 8, 9, 10]
...
If you assign the variable like this:
numLength = len(numbers) #measures the length of the array
numbersrev = [0]*numLength #blank array for reverse
You'll get the correct answer since the reverse list is no longer pointing to the normal list.
the problem you are facing is because of this line
numbersrev = numbers #blank array for reverse
In this line you are not creating an empty array, you are saving the reference of numbers array in a new variable named numbersrev. This means that when you make an operation in array numbersrev you are changing also the values of numbers. To avoid this kind of problems you have two options:
Make a copy of the array with slices
In this way you copy the values of the array, not the reference of the array. This means the changes you make to the new array will not change the original array.
numbersrev = numbers[:]
Create an empty array and use append instead of assignation
This change is a little different from what you did, but basically instead of creating a copy of the array, you create a new array which will be filled in the for loop, something like:
numbers.rev = []
...
for k in range(8):
numbersrev.append(numbers[i])
So with the first option and also changing some things in the k index we have a code like this:
numbers = [2, 4, 9, 11, 8, 3, 2, 5, 10] # you can change for read
numbersrev = numbers[:] #copy array elements
numLength = len(numbers) #array length
print(numbers)
print("Array length of numbers: ", numLength)
i = numLength - 1
# you don't need to initialize k because it's initialized and incremented with range in the loop
for k in range(numLength):
numbersrev[k] = numbers[i]
print ("The ", i," element of numbers is the ", k," element of numbersrev")
i -= 1
print(numbersrev)
Hope this would help you solve the problem, just like a note you could solve this problem in many ways: slicing (numbers[::-1]), list comprehension and some others. All the ways are valid so just in case you want to explore more.
I am trying to solve an issue that I am currently running into. I want to have to have a list that is made up of only random integers. Then if i find a duplicate integer within this list i want to minus the rest of the list by one, after the second time the duplicate number appeared. Furthermore if a second pair of duplicate numbers are encountered, it should then minus the rest of the list by two, than if a third by three and etc.
But it should not affect the same duplicate number or any other duplicated number (that differs from the first) that is in the sequence.
For example
mylist = [0 1 2 3 4 5 6 2 8 5 10 11 12 1 14 15 16 17]
I want the end result to look like;
mylist = [0 1 2 3 4 5 6 2 7 5 9 10 11 1 12 13 14 15]
I have some rough code that I created to attempt this, but it will always minus the whole list including duplicated integers (the first pairs and any further pairs).
If someone can shed some light on how to deal with this problem i will be highly grateful!
Sorry forgot to add my code
a = [49, 51, 53, 56, 49, 54, 53, 48]
dupes = list()
number = 1
print (dupes)
while True:
#move integers from a to dupes (one by one)
for i in a[:]:
if i >= 2:
dupes.append(i)
a.remove(i)
if dupes in a:
a = [x - number for x in a]
print (dupes)
print(dupes)
if dupes in a:
a = [x - number for x in a]
number = number+1
break
Forgot to mention earlier, me and friend are currently working on this problem and the code i supplied is our rough outline of what is should look like and now the end result, I know that it does now work so i decided to ask for help for the issue
You need to iterate through your list and when you encounter a duplicate(can use list slicing) then decrement the next item!
List slicing - example,
>>> L=[2,4,6,8,10]
>>> L[1:5] # all elements from index 1 to 5
[4, 6, 8, 10]
>>> L[3:] # all elements from index 3 till the end of list
[8, 10]
>>> L[:2] # all elements from index beginning of list to second element
[2, 4]
>>> L[:-2] # all elements from index beginning of list to last second element
[2, 4, 6]
>>> L[::-1] # reverse the list
[10, 8, 6, 4, 2]
And enumerate
returns a tuple containing a count (from start which defaults to 0)
and the values obtained from iterating over sequence
Therefore,
mylist=[0, 1, 2, 3, 4, 5, 6, 2, 8, 5, 10, 11, 12, 1, 14, 15, 16, 17]
dup=0
for index,i in enumerate(mylist):
if i in mylist[:index]:
dup+=1
else:
mylist[index]-=dup
print mylist
Output:
[0, 1, 2, 3, 4, 5, 6, 2, 7, 5, 8, 9, 10, 1, 11, 12, 13, 14]
numbers=[i**3 for i in range (10) if i**3%3==1]
print(numbers)
#gets 1,64,343
Why is 1, 64, 343 the answer?
This is equivalent to the code:
for i in range(10):
if (i*i*i) % 3 == 1:
numbers.append(i*i*i)
print (numbers)
You are checking if the remainder obtained when the cube of a number from 1 to 10 is divided by 3 is equal to 1. If it is, you are adding it to a list and printing it.
The meaning of **
ex: 2**3= 2*2*2 #this means 2 to the power 3 = 8
The meaning of %
ex: 5%2= 1 #the sign means module, that means the remaining value after divide 5 by 2, it is one.
in your way, the correct path to write the for each is
for i in range(0,10):
value = i**3
if(value%3 == 1):
print("the value is {0}".format(value))
so the result is :
the value is 1
the value is 64
the value is 343
bit explanation inside the for loop
first get the i = 0, at this point value = 0*0*0 = 0, then value%3=0
then get the i=1, at this point value = 1*1*1 = 1 ,the 'value%3' means 1%3 = 1, so the answer i 1
.... like this see about other conditions also. hope this will help to you.
first i is in [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
then if (i*i*i) rem 3 is equal to 1
it selects (i*i*i)
and for [1,4,7]: (1*1*1)%3==1, (4*4*4)%3==1 and (7*7*7)%3==1:
1*1*1=1 and 1/3=0 :remainder=1
4*4*4=64 and 64/3=21 :remainder=1
7*7*7=343 and 343/3=114 :remainder=1
so the output is:
[1*1*1, 4*4*4, 7*7*7] which is [1, 64, 343]
your code:
numbers=[i**3 for i in range (10) if i**3%3==1]
print(numbers)
and this code:
numbers=[]
for i in [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]:
if (i*i*i) % 3 == 1:
numbers.append(i*i*i)
print(numbers)
output this:
[1, 64, 343]