I need to design a function that takes a list of int as a parameter and in that list we look at the last element it will have some value v, Then we take v cards from the top of the deck and put them above the bottom most card in the deck.
Here is an example:
>>> test_list = [1, 28, 3, 4, 27, 8, 7, 6, 5]
>>> insert_top_to_bottom(test_list)
>>> test_list = [8, 7, 6, 1, 28, 3, 4, 27, 5]
value = deck[-1] # value of the last element
I believe this will do
def insert_top_to_bottom(test_list, v):
return test_list[v : -1] + test_list[:v] + [test_list[-1]]
test_list = [1, 28, 3, 4, 27, 8, 7, 6, 5]
test_list = insert_top_to_bottom(test_list, 5)
print test_list
Here is my attempt, though you should next time show your own attempts.
Please note that I did not include any type of checking or avoiding errors. Just the basics.
def top_to_bottom(l):
last = l[len(l)-1]
move_these = l[:last]
move_these.append(l[len(l)-1])
del l[:last]
del l[len(l)-1]
l.extend(move_these)
I hope I could help.
EDIT
I didn't make it a one-line funtion so you can understand a bit better.
Since you asked, here is some more explanaition about slicing.
my_list[x:y] is a basic slice. This will output my_list from index x to (but excluding index y).
You can leave out either, which will just fill up that part as far as possible.
For example, using
my_list[:5]
will give you my_list from the beginning to (but excluding!) index 5.
If the list is [0, 1, 2, 3, 4, 5, 6], it will give [0, 1, 2, 3, 4]. If you want to get a list from a certain index until the end, you leave out the 'y' part. Like so:
my_list[3:]
Applying that on the list previously stated, you will get [3, 4, 5, 6].
I hope you understood! Comment if you have any more questions.
Related
I wanted to create a list of lists
def func(m,n,l,t):
a=[i for i in range(m)]
b=[]
b.append(a)
a=swap(a)
b.append(a)
for i in b:
print(i)
def swap(l):
i,n=0,len(l)
while(i<n):
l[i],l[i+1]=l[i+1],l[i]
i+=2
return l
i created list a as my basis and append each modification i want to in the b list.
The problem is after i append the first list and modify it, the first one i inserted also changes the same as the second one i inserted.
the output of this code is this
[1, 0, 3, 2, 5, 4, 7, 6]
[1, 0, 3, 2, 5, 4, 7, 6]
what i want is when i performm the swap i dont want the first list to change
the output should look like this
[0, 1, 2, 3, 4, 5, 6, 7]
[1, 0, 3, 2, 5, 4, 7, 6]
if you could help, please thank you.
In place of append() method, you can try out extend() method which does the same as of append. but extend() method is mainly used for iterate items, as of in your case.
and also if you want to use only append, then take a copy of your variable first and append it, as you are changing in the first place, it is taking effect in the list too. So you can follow append into a list like, a.copy(). Hopes it helps
Please try out and share the feedback.
Thank you
Their are multiple code error you have done, for example:
you don't have to iterate over range object in order to get list, you could just...
a = list(range(m))
and also You don't have to run a while loop in order to iterate over two steps you could...
for i in range(0, len(l), 2):
see range for reference
also you didn't use any of n, l, t parameters of the func function.
THE ANSWER
when you pass the a variable to the swap function you are actually passing the class Object itself, so the swap function can change the value of the a variable (list class).
All you need is passing a copy of the a variable not the variable itself
a = swap(a.copy())
FINAL CODE
def swap(l):
for i in range(0, len(l), 2):
l[i],l[i+1]=l[i+1],l[i]
return l
def func(m):
a = list(range(m))
b = [a]
a = swap(a.copy())
b.append(a)
for i in b:
print(i)
func(8) # [0, 1, 2, 3, 4, 5, 6, 7], [1, 0, 3, 2, 5, 4, 7, 6]
Update: Wow that was fast! Thank you all for your input, totally overlooked that I was comparing an indice to a value. I’m aware of the shorter methods, the course that I’m on hasn’t shown us the “set” method so I wanted to accomplish the assignment with what was taught. I would upvote all of your comments if I had enough reputation points, thank you all so much!!
First off im an uber-noob to python. I'm learning for the sake of improving my networking administrating skills. The purpose of my code is to remove integers that occur more than once, and to create a new array that doesn't have any duplicate integers. However my code adds in all duplicates, instead of skipping them.
Code:
import array
my_list = [1,1,1,1, 2, 3, 3, 4, 5, 6, 6, 3, 7]
unique_list = []
dupe_list = []
for i in range(len(my_list)):
if i not in unique_list:
unique_list.append(my_list[i])
print("unique array is ", unique_list, 'n', "dupe list: ", dupe_list)
Output:
unique array is [1, 1, 1, 2, 3, 3, 4, 5, 6, 6, 3, 7] n dupe list: []
So basically I have a for-loop that checks whether an integer that is in my_list is also in unique_list, if not then I want the code to add the integer in. However my code is adding every integer into the unique-list regardless of whether it is in there or not. Does the not in function not work the way I think it does?
you can remove integers that occur more then once using set
>>> my_list = [1,1,1,1, 2, 3, 3, 4, 5, 6, 6, 3, 7]
>>> list(set(my_list))
[1, 2, 3, 4, 5, 6, 7]
>>>
As others have pointed out, there are easier ways to accomplish what you're trying to do, but your bug is because you're confusing indices and values.
for i in range(len(my_list)):
if i not in unique_list:
unique_list.append(my_list[i])
In the above code, i is the index of each item in my_list, but you're comparing it against the values in unique_list. One of these things is not like the other!
If you wanted to iterate through by index, you need to use the subscript operator [] to get the value out of my_list:
for i in range(len(my_list)):
if my_list[i] not in unique_list:
unique_list.append(my_list[i])
It would however be simpler to just iterate through by value:
for v in my_list:
if v not in unique_list:
unique_list.append(v)
And of course it's simpler yet to just do:
unique_list = list(set(my_list))
You can use the function dict.fromkeys() to remove duplicates
>>> my_list = [1,1,1,1, 2, 3, 3, 4, 5, 6, 6, 3, 7]
>>> list(dict.fromkeys(my_list))
[1, 2, 3, 4, 5, 6, 7]
I need to perform a triple cut. My function takes as parameter list of int, and somewhere in that list there is 27 and 28. What I need to do is to check what comes first 27 or 28, everything before the 27 or 28(depending upon what comes first) goes to the bottom of the list and everything after 27 or 28 (depending upon what comes second will go the top of the list.
Here is an example:
>>> test_list = [1, 28, 3, 4, 27, 5, 6, 7, 8]
>>> triple_cut(test_list)
>>> test_list = [5, 6, 7, 8, 28, 3, 4, 27, 1]
Here is what I have so far
#find the index positions of the two jokers
find_joker1 = deck.index(27)
find_joker2 = deck.index(28)
print(find_joker1, find_joker2)
new_list = []
# use selection to check which joker occurs first
if(find_joker1 > find_joker2): # joker2(28) occurs first in the list
# loop throgh each element in the list that occurs before Joker1
# and move them to the end of the list
# move element that occur after Joker1(27) to the top of the list
for i in deck:
if(deck.index(i) > find_joker1): # elements that occur after second joker
new_list.append(i) # move those element to the top of the list
new_list.append(28) # add Joker2
for i in deck: # element between the two Jokers
if(deck.index(i) > find_joker2 and deck.index(i) < find_joker1):
new_list.append(i)
new_list.append(27)
for i in deck: # elements before the first joker
if(deck.index(i) < find_joker2):
new_list.append(i)
print(new_list)
Can be solved by slicing.
def triple_cut(lst):
a=lst.index(27)
b=lst.index(28)
if a>b:
return lst[a+1:]+ lst[b:a+1]+ lst[:b]
else:
return lst[b+1:]+ lst[a:b+1]+ lst[:a]
What actually happenning:
Slice everything after the bigger indexed one.
Slice from the lower indexed one to upper indexed one.
Slice everything before the lower indexed one.
Add all together.
N.B: During slicing, first index is inclusive and second index is exclusive.
Demo,
>>test_list = [1, 28, 3, 4, 27, 5, 6, 7, 8]
>>tripplecut(test_list)
[5, 6, 7, 8, 27, 3, 4, 28, 1]
Some explanation:
By slicing, you can get a part of a list. First see, how actually slicing works:
lst[start:end:increment]
For relevance to your question, skip the increment part. Default increment is 1, just what we need. So, we will slice a list like below:
lst[start:end]
Let's do some experiments with your given list.
>>> test_list = [1, 28, 3, 4, 27, 5, 6, 7, 8]
Say, we want a list from index 2(3) to index 5(27). Simply do:
>>> test_list[2:6]
[3,4,27]
Why I've used 6 in place of 5. That's because:
In case of slicing, the start index is inclusive, but the end index is exclusive.
What if we want a list from start to index 4(27). Do:
>> test_list[:5]
[1,28,3,4,27]
If we want index 3 to end? Simply do:
>>test_list[3:]
[4, 27, 5, 6, 7, 8]
Hope that will help you a bit.
Since you don't actually need to know which is which, you can just find the two indexes and slice.
def triple_cut(lst):
first, second = [i for i, e in enumerate(lst) if e in [27, 28]]
return lst[second+1:] + lst[first:second+1]+ lst[:first]
I'm supposed to write a program that contracts a list. For example:
[1, 3, 4, 5, 1, 8, 6,]
has to be contracted into a list that looks like this:
[1, 5, 1, 8, 6, 6]
I don't have a clue on how to do this and was hoping that any of you could help me.
I'm given a list that looks like this l1 = [1, 3, 9, 1, 2, 7, 8] and i'm supposed to contract the list into a new list which consist of the old lists nondecending segments extremities. It is supposed to work with any list given.
Let me try to understand the question:
im given a list that looks like this l1 = [1, 3, 9, 1, 2, 7, 8] and im
supposed to contract the list by taking the first number then the next
bigest and the smallest after that and then the biggest again. This is
a school assignment and i have no idea on how to do this. english
isn't my first language and it's realy difficult to explain the
assignment :/
I supposed that "biggest" and "smallest" refer to local maxima and minima. So a number at index i is "biggest" if l[i-1] < l[i] and l[i] > l[i+1]. "Smallest" the other way around. So basically your are searching for the extrema of a function N -> N.
If this is what you want, this should help (considering that start and end points always are extrema):
#! /usr/bin/python3.2
def sign(x): return 0 if not x else x // abs(x)
def extrema (l):
return [l[0]] + [e for i, e in enumerate(l[:-1]) if i and sign(e-l[i-1])==sign(e-l[i+1])] + [l[-1]]
l1 = [1, 3, 9, 1, 2, 7, 8]
print (extrema (l1))
I am trying to write a code that changes the position of an integer inside a list (basically swaps the position with another integer)
I have tried to use all logic, but still can't understand why my code is messing up:
SpecialNum = 10
def number_move(move_number):
for elements in range(len(move_number)):
if ( SpecialNum != move_number[-1]):
x = move_number.index(SpecialNum)
y = move_number.index(SpecialNum)+1
move_number[y], move_number[x] = move_number[x], move_number[y]
return (move_number)
the output should be:
[1,2,3,10,4,5,6]
>>>[1,2,3,4,10,5,6]
but output comes as:
[1,2,3,4,5,6,10]
Assuming your actual indentation looks like this:
SpecialNum = 10
def number_move(move_number):
for elements in range(len(move_number)):
if ( SpecialNum != move_number[-1]):
x = move_number.index(SpecialNum)
y = move_number.index(SpecialNum)+1
move_number[y], move_number[x] = move_number[x], move_number[y]
return move_number
… the problem is that you're swapping the 10 to the right over and over in a loop, until it reaches the very end.
If that isn't what you want, why do you have the for elements in range(len(move_number)) in the first place? Just take it out, and it will only get swapped right once.
As a side note, you rarely need range(len(eggs)); you can just do for egg in eggs (or, if you need the index along with the actual object, for index, egg in enumerate(eggs)).
Also, you've got a whole lot of extra parentheses that aren't needed, and make the code harder to read.
Meanwhile, every call to index has to search the entire list to find your object's position; if you already know the position, it's better to just use it. Not only is it a lot faster, and simpler, it's also more robust—if there are two elements of the list with the same value, index can only find the first one. In your case, there's no obvious way around using index, but at least you can avoid calling it twice.
Putting that together:
SpecialNum = 10
def number_move(move_number):
x = move_number.index(SpecialNum)
y = x + 1
if y != len(move_number):
move_number[y], move_number[x] = move_number[x], move_number[y]
Finally, I said there's no obvious way around using index… but is there a non-obvious way? Sure. If you're going to call index repeatedly on the same object, we can make the last-found index part of the interface to the function, or we can even store a cache inside the function. The simplest way to do this is to turn the whole thing into a generator. A generator that mutates its arguments can be kind of confusing, so let's make it return copies instead. And finally, to make it customizable, let's take a parameter so you can specify a different SpecialNum than 10.
SpecialNum = 10
def number_move(move_number, special_num=SpecialNum):
for x, element in reversed(list(enumerate(move_number))):
if element == special_num:
while x+1 < len(move_number):
move_number = (move_number[:x] +
[move_number[x+1], move_number[x]] +
move_number[x+2:])
yield move_number
x += 1
Now, it'll move all of the 10s to the end, one step at a time. Like this:
>>> n = [1, 10, 2, 3, 10, 4, 5, 6]
>>> for x in number_move(n):
... print(x)
[1, 10, 2, 3, 4, 10, 5, 6]
[1, 10, 2, 3, 4, 5, 10, 6]
[1, 10, 2, 3, 4, 5, 6, 10]
[1, 2, 10, 3, 4, 5, 6, 10]
[1, 2, 3, 10, 4, 5, 6, 10]
[1, 2, 3, 4, 10, 5, 6, 10]
[1, 2, 3, 4, 5, 10, 6, 10]
[1, 2, 3, 4, 5, 6, 10, 10]
[1, 2, 3, 4, 5, 6, 10, 10]
You don't need the for loop :)
def number_move(move_number):
x = move_number.index(SpecialNum)
y = move_number.index(SpecialNum)+1
move_number[y], move_number[x] = move_number[x], move_number[y]
Alternative:
>>> def number_move(m, i):
num = m.pop(i)
m.insert(i+1, num)
return m
>>> l = number_move([1,2,3,10,4,5,6], 3)
>>> l
[1, 2, 3, 4, 10, 5, 6]