one problem statement on python list arithmetic that is I have one list say,
my_set = [24565,24621,32,598,899]
I want to take difference between first two elements and if difference is in range of -127 to 127 then add +128 to next index position and so on. First element of the list stays as-is
Like this the output
[24565, 56, +128, 24589, +128, −566, +128, -301].
this is what I tried to do
def inRange(val):
return val in range(-127,127)
my_set = [24565,24621,32,598,899]
for i in range(len(my_set)-1):
res = my_set[i] - my_set[i+1]
if inRange(res) == True:
my_set.insert(i+1,res)
my_set.insert(i+2,128)
print(my_set)
Please tell me how to do that.??
THankyou!
But in the desired output that you have written, you are adding 128 in spite of what the difference is. And you are also adding the difference value to the list. I got confused there. Anyways, this does adding the difference and 128 to the list. Also, can you make use of a new list to update the output or should you update the same list? The first case, its easy; the second case, you can try the below code
def inRange(val): # a simple if is enough and faster range
if val < 127 and val >= -127:
return True
my_set = [24565,24621,32,598,899]
#as you wanted the first element as is, moved this piece of code out of the loop
my_set.insert(1, my_set[0]-my_set[1])
my_set.insert(2,128)
i = 3
while i < len(my_set) - 1:
res = my_set[i] - my_set[i+1]
#if inRange(res) == True: # you can add this loop if required and do the inserts accordingly
my_set[i] = res
my_set.insert(i+1, 128)
i = i + 2 # if you are gonna add the if check and insert only then, you might want to change the i increment as per that
print(my_set)
Hope this helps. And expecting some one else to give a better answer :)
Related
I have been working on this assignment for about 2 weeks and have nothing done. I am a starter at coding and my teacher is really not helping me with it. She redirects me to her videos that I have to learn from every time and will not directly tell or help me on how I can do it. Here are the instructions to the assignment (said in a video, but made it into text.
Find the mean
Create a program that finds the mean of a list of numbers.
Iterate through it, and instead of printing each item, you want to add them together.
Create a new variable inside of that, that takes the grand total when you add things together,
And then you have to divide it by the length of your array, for python/java script you’ll need to use the method that lets you know the length of your list.
Bonus point for kids who can find the median, to do that you need to sort your list and then you need to remove items from the right and the left until you only have one left
All you’re doing is you need to create a variable that is your list
Create another variable that is a empty one at the moment and be a number
Iterate through your list and add each of the numbers to the variable you created
Then divide the number by the number of items that you had in the list.
Here's what I've done so far.
num = [1, 2, 3, 4, 5, 6];
total = 0;
total = (num[0] + total)
total = (num[1] + total)
total = (num[2] + total)
total = (num[3] + total)
total = (num[4] + total)
total = (num[5] + total)
print(total)
However, she tells me I need to shorten down the total = (num[_] + total) parts into a loop. Here is how she is telling me to do a loop in one of her videos.
for x in ____: print(x)
or
for x in range(len(___)): print (x+1, ____[x])
there is also
while i < len(___):
print(___[i])
i = i + 1
I really don't understand how to do this so explain it like I'm a total noob.
First of all, loops in python are of two types.
while: a while loop executes the code in a body until a condition is true. For example:
i = 0
while(i < 5):
i = i + 1
executes i = i + 1 until i < 5 is true, meaning that when i will be equal to 5 the loop will terminate because its condition becomes false.
for: a for loop in python iterates over the items of any sequence, from the first to the last, and execute its body at each iteration.
Note: in both cases, by loop body I mean the indented code, in the example above the body is i = i + 5.
Iterating over a list. You can iterate over a list:
Using an index
As each position of the array is indexed with a positive number from 0 to the length of the array minus 1, you can access the positions of the array with an incremental index. So, for example:
i = 0
while i < len(arr):
print(arr[i])
i = i + 1
will access arr[0] in the first iteration, arr[1] in the second iteration and so on, up to arr[len(arr)-1] in the last iteration. Then, when i is further incremented, i = len(arr) and so the condition in the while loop (i < arr[i]) becomes false. So the loop is broken.
Using an iterator
I won't go in the details of how an iterator works under the surface since it may be too much to absorb for a beginner. However, what matters to you is the following. In Python you can use an iterator to write the condition of a for loop, as your teacher showed you in the example:
for x in arr:
print(x)
An iterator is intuitively an object that iterates over something that has the characteristic of being "iterable". Lists are not the only iterable elements in python, however they are probably the most important to know. Using an iterator on a list allows you to access in order all the elements of the list. The value of the element of the list is stored in the variable x at each iteration. Therefore:
iter 1: x = arr[0]
iter 2: x = arr[1]
...
iter len(arr)-1: x = arr[len(arr)-1]
Once all the elements of the list are accessed, the loop terminates.
Note: in python, the function range(n) creates an "iterable" from 0 to n-1, so the for loop
for i in range(len(arr)):
print(arr[i])
uses an iterator to create the sequence of values stored in i and then i is in turn used on the array arr to access its elements positionally.
Summing the elements. If you understand what I explained to you, it should be straightforward to write a loop to sum all the elements of a list. You initialize a variable sum=0 before the loop. Then, you add the element accessed as we saw above at each iteration to the variable sum. It will be something like:
sum = 0
for x in arr:
sum = sum + x
I will let you write an equivalent code with the other two methods I showed you and do the other points of the assignment by yourself. I am sure that once you'll understand how it works you'll be fine. I hope to have answered your question.
She wants you to loop through the list.
Python is really nice makes this easier than other languages.
I have an example below that is close to what you need but I do not want to do your homework for you.
listName = [4,8,4,7,84]
for currentListValue in listName:
#Do your calculating here...
#Example: tempVar = tempVar + (currentListValue * 2)
as mentioned in the comments w3schools is a good reference for python.
So I wrote this algorithm where given a set of integers it will remove all integers except 0 and 7 and then it will check if the remaining integers are in a certain order and then will return a boolean. Code below:
def spy_game(nums):
for i in nums:
if i != 0:
if i == 7:
continue
else:
nums.remove(i)
else:
continue
stringlist = [str(o) for o in nums]
mystring = ''.join(stringlist)
return '007' in mystring
spy_game([1,0,2,4,0,7,5])
Now the problem is that if I run
(for example) spy_game([1,0,2,4,0,7,5]) it will not return True regardless of the fact that the sequence of interest is present. After I decided to return the list per se after the filtration process, I found that all numbers except the ones in the middle got filtered out. So in this example, if I return nums it will return [0, 4, 0, 7] although the 4 should've been removed. I am aware that there are more optimal alternatives to this algorithm but I just want to understand why it doesn't work. Thank you.
Instead of modifying the list, use another list to keep track of the wanted numbers.
You should not modify the list while iterating on it.
Here's a cleaned up version
def spy_game(nums):
ans = []
for i in nums:
if i == 0 or i == 7:
ans.append(i)
stringlist = [str(o) for o in ans]
mystring = ''.join(stringlist)
return '007' in mystring
zenwraight's comment says what the problem is: in Python, you can't modify a list while iterating over it.
As for why, the Python documentation discusses this in a note on the for statement's section:
An internal counter is used to keep track of which item is used next, and this is incremented on each iteration. … This means that if the [loop body] deletes the current … item from the sequence, the next item will be skipped (since it gets the index of the current item which has already been treated).
The documentation also describes what happens when you insert an element during a loop, and suggests one possible solution (using a slice to copy the list: for i in nums[:]: ...). In your use case, that solution is likely to work fine, but it is considerably less efficient than options that don't copy the entire list.
A better solution might be to use another list comprehension:
nums = [i for i in nums if i == 0 or i == 7]
what I'm trying to do is a function for which you give a number and it lists every string of that length that contain numbers in ascending order with the conditions that I cannot have more than 4 time the same number and my number have to be consecutive. If possible, I would like not to list numbers that are anacycle of those already listed.
For example:
fct(5) gives me
"11112
11122
11123
11223 (11222 is omitted)
11233
11234
12223
12234
12345"
Do you think it is better to do this with regex and generate every combination or to increment the numbers in a list while I browse my initial list and modify it ?
Are other languages better to do something like this ?
EDIT : Sorry I think it wasn't enough clear.
I tried at beginning to do something like :
ls = list("1111222233334")
i = -1
while ls[0] == "1":
print("".join(ls))
if int(ls[i]) == int(ls[i-1]) and int(ls[i]) < 9:
ls[i] = str(int(ls[i])+1)
else:
i -= 1
Of course this doesn't work and I think there is too much condition if I go this way, this is why I ask if there is something already done in Python that can list every ascending number of specific length.
We cannot go over 9 so this function does anything if called with 37.
By anacycle I mean something that gives something else if read starting by the end (like "roma" and "amor").
Is it better to generate a list of every number of that length and then delete all those that do not correspond, + delete those that are equivalent to those that are already in ?
I finally decided to do this :
def handlisting(n):
L = [str(i) for i in range (int("1"*n), int(12345679 * (10**(n-8))) + 1)]
reg = "1{1,4}(2{1,4}(3{1,4}(4{1,4}(5{1,4}(6{1,4}(7{1,4}(8{1,4}(9{1,4})?)?)?)?)?)?)?)?\Z"
return list(filter(re.compile(reg).match, L))
I think that I can do better because this one is pretty slow, and it doesn't delete those that are the same than other if read starting by the end (it will return "112234" and "123344", or "11112" and "12222")
EDIT : It goes a little faster with a condition like this :
L = [str(i) for i in range (int("1"*n), int(12345679 * (10**(n-8))) + 1) if \
i%10 >= (i//10)%10 and i%100 >= (i//10)%100 and i%1000 >= (i//10)%1000 and i%10000 >= (i//10)%10000]
can someone explain me the loop below:
for item in aList[start:end:1]:
aList[aList.index(item)],aList[aList.index(item)+1] = aList[aList.index(item)],aList[aList.index(item)+1]
Let s say aList = [5,2,3,6,1]. the first iteration the index would 0. in the second would be again 0 and after will be increased to 2. Thus it will choose [5,2] and will continue with [2,5] [3,6]....
Why is that and what is the right way to do this?
UPDATE: The above is just an example of a specific behavior I wanted to understand. the actual code tries to implement a coctail sort algorithm.
The actual code is like that
if f == 1:
for d in area[start:end:f]:
print area,f ,d,area[area.index(d)+1],area.index(d)+1
if d > area[area.index(d)+1]:
tmp = area.index(d)
area[tmp], area[tmp+1] = area[tmp+1],area[tmp]
area=area
end -= 1
f = ~f + 1
if f == -1:
for d in area[end:start:f]:
print area,f,d,area[area.index(d)-1],area.index(d)-1
if d < area[area.index(d)-1]:
tmp = area.index(d)
area[tmp], area[tmp-1] = area[tmp-1], area[tmp]
area=area
start += 1
f = ~f + 1
As John said, it is a bad idea to mutate a list whilst looping over it. Instead create a copy of the list and change the data in the copy of the list. However it's also unclear what you are trying to achieve so if you specify that there might be a better way to do it.
Before diving into the problem, it's worth noting there are a couple issues with the code as written.
start and end aren't defined
The assignment in the for-loop doesn't actually swap the order
Those things aside, the algorithm fails, because you iterate over every item. I'll go through 2 iterations to show you why that's so.
Let's use your example:
aList = [5,2,3,6,1]
First iteration:
item = 5
aList.index(5) = 0
so, we're going to swap aList[0] and aList[1]
at the end of the iteration, aList = [2,5,3,6,1]
Second iteration:
item = 2
Wait, why 2? Because the slice operation creates a new list that we're iterating over, and that list has the original order.
aList.index(2) = 0
so, we're going to swap aList[0] and aList[1]... again.
at the end of the iteration, aList = [5,2,3,6,1]... which is the original order.
And so on
On the third iteration, we'll wind up swapping the 3rd and 4th items. And on the fourth iteration of the loop, we'll swap them again.
You can verify this is the case by adding a print statement to your for-loop: print aList.
So how do you fix it?
You can get around this behavior by iterating over every other item, instead of every item. Try changing the slice stride (step) to 2, instead of 1.
I want to know if is it possible to change the value of the iterator in its for-loop?
For example I want to write a program to calculate prime factor of a number in the below way :
def primeFactors(number):
for i in range(2,number+1):
if (number%i==0)
print(i,end=',')
number=number/i
i=i-1 #to check that factor again!
My question : Is it possible to change the last two line in a way that when I change i and number in the if block, their value change in the for loop!
Update: Defining the iterator as a global variable, could help me? Why?
Short answer (like Daniel Roseman's): No
Long answer: No, but this does what you want:
def redo_range(start, end):
while start < end:
start += 1
redo = (yield start)
if redo:
start -= 2
redone_5 = False
r = redo_range(2, 10)
for i in r:
print(i)
if i == 5 and not redone_5:
r.send(True)
redone_5 = True
Output:
3
4
5
5
6
7
8
9
10
As you can see, 5 gets repeated. It used a generator function which allows the last value of the index variable to be repeated. There are simpler methods (while loops, list of values to check, etc.) but this one matches you code the closest.
No.
Python's for loop is like other languages' foreach loops. Your i variable is not a counter, it is the value of each element in a list, in this case the list of numbers between 2 and number+1. Even if you changed the value, that would not change what was the next element in that list.
The standard way of dealing with this is to completely exhaust the divisions by i in the body of the for loop itself:
def primeFactors(number):
for i in range(2,number+1):
while number % i == 0:
print(i, end=',')
number /= i
It's slightly more efficient to do the division and remainder in one step:
def primeFactors(number):
for i in range(2, number+1):
while True:
q, r = divmod(number, i)
if r != 0:
break
print(i, end=',')
number = q
The only way to change the next value yielded is to somehow tell the iterable what the next value to yield should be. With a lot of standard iterables, this isn't possible. however, you can do it with a specially coded generator:
def crazy_iter(iterable):
iterable = iter(iterable)
for item in iterable:
sent = yield item
if sent is not None:
yield None # Return value of `iterable.send(...)`
yield sent
num = 10
iterable = crazy_iter(range(2, 11))
for i in iterable:
if not num%i:
print i
num /= i
if i > 2:
iterable.send(i-1)
I would definitely not argue that this is easier to read than the equivalent while loop, but it does demonstrate sending stuff to a generator which may gain your team points at your next local programming trivia night.
It is not possible the way you are doing it. The for loop variable can be changed inside each loop iteration, like this:
for a in range (1, 6):
print a
a = a + 1
print a
print
The resulting output is:
1
2
2
3
3
4
4
5
5
6
It does get modified inside each for loop iteration.
The reason for the behavior displayed by Python's for loop is that, at the beginning of each iteration, the for loop variable is assinged the next unused value from the specified iterator. Therefore, whatever changes you make to the for loop variable get effectively destroyed at the beginning of each iteration.
To achieve what I think you may be needing, you should probably use a while loop, providing your own counter variable, your own increment code and any special case modifications for it you may need inside your loop. Example:
a = 1
while a <= 5:
print a
if a == 3:
a = a + 1
a = a + 1
print a
print
The resulting output is:
1
2
2
3
3
5
5
6
Yes, we can only if we dont change the reference of the object that we are using. If we can edit the number by accessing the reference of number variable, then what you asked is possible.
A simple example:
a=[1,2,3]
a=a+[4]==>here, a new object is created which plots to different address.
a+=[4]==>here , the same object is getting updated which give us the desired result.
number=10
list1=list(range(2,number+1))
# list1
for i in list1:
print(list1,i)
if (number%i==0):
print(i,end=',')
number=number//i #we can simply replace it with number//=i to edit the number without changing the reference or without creating a new object.
try:
[list1.pop() for i in range(10,0,-1) if(i>number)]
#here pop() method is working on the same object which list created by number refers. so, we can able to change the iterable in the forloop.
except:
continue
i=i-1 #to check that factor again!