I got an answer to my task in python.But I had a dought that
what is the difference between the empty list inside the function and outside the function?
why i am asking this because, my expected output came while i am declaring the empty list outside the function but not inside the function. Also I dont know how the output value in my list (5230) came in my output ? first three output value i know what happening in my program but last value in my list i dont know how that value came ?
my task question is
A 10-SUBSTRING OF A NUMBER IS A SUBSTRING OF ITS DIGITS THAT SUM UPTO 10 ALSO BE IN A ORDERED SEQUENCE OF THE GIVEN STRING ?
THE STRING IS '3523014'
EXPECTED OUTPUT : ['352','523','23014','5230']
MY CODE USING RECURSION.
l=[]
def ten_str(s):
x,z,p=0,1,1
for i in s:
x+=eval(i)
if x==10:
l.append(s[:z])
ten_str(s[p:])
p+=1
z+=1
return l
print(ten_str('3523014'))
If you create the empty list inside the function, then each time the function is recursively called it will create a new empty list. Strings meeting the condition will be lost when that recursive call ends, rather than being added to the global list. I find http://pythontutor.com/ helps a lot with recursion exercises.
Based on your question, a 10-substring is a set of values in the original order of the list when added sum up to 10.
5+2+3+0 = 10. Your list 3523014 contains 5230
Related
I am trying to learn about while and for loops. This function prints out the highest number in a list. But, I'm not entirely sure how it works. Can anyone break down how it works for me. Maybe step by step and/or with a flowchart. I'm struggling and want to learn.
def highest_number(list_tested):
x=list_tested[0]
for number in list_tested:
if x<number:
x=number
print(x)
highest_number([1,5,3,2,3,4,5,8,5,21,2,8,9,3])
One of the most helpful things for understanding new code is going through it step by step:
PythonTutor has a visualizer: Paste in your code and hit visualize execution.
What this is going form the first to the last number and saying:
Is this new number bigger than the one I have? If so, keep the new number, if not keep the old number.
At the end, x will be the largest number.
See my comments for step by step explanation of each line
def highest_number(list_tested): # function defined to take a list
x=list_tested[0] # x is assigned the value of first element of list
for number in list_tested: # iterate over all the elements of input list
if x<number: # if value in 'x' is smaller than the current number
x=number # then store the value of current element in 'x'
print(x) # after iteration complete, print the value of 'x'
highest_number([1,5,3,2,3,4,5,8,5,21,2,8,9,3]) # just call to the function defined above
So basically, the function finds the largest number in the list by value.
It starts by setting the large number (x) as the first element of list, and then keeps comparing it to other elements of the list, until it finds an element which is greater than the largest number found till now (which is stored in x). So at the end, the largest value is stored in x.
Looks like you are new to the programming world. Maybe you should start with some basic concepts, for/while loops are some among which, that would be helpful for you before jumping into something like this.
Here is one of the explanations you may easily find on the Internet http://www.teamten.com/lawrence/programming/intro/intro8.html
My problem is, that need a list with length of 6:
list=[[],[],[],[],[],[]]
Ok, that's not difficult. Next I'm going to insert integers into the list:
list=[[60],[47],[0],[47],[],[]]
Here comes the real problem: How can I now extend the lists and fill them again and so on, so that it looks something like that:
list=[[60,47,13],[47,13,8],[1,3,1],[13,8,5],[],[]]
I can't find a solution, because at the beginning i do not know the length of each list, I know, they are all the same, but I'm not able to say what length exactly they will have at the end, so I'm forced to add an element to each of these lists, but for some reason i can't.
Btw: This is not a homework, it's part of a private project :)
You don't. You use normal list operations to add elements.
L[0].append(47)
Don't use the name list for your variable it conflicts with the built-in function list()
my_list = [[],[],[],[],[],[]]
my_list[0].append(60)
my_list[1].append(47)
my_list[2].append(0)
my_list[3].append(47)
print my_list # prints [[60],[47],[0],[47],[],[]]
Below are snippets of code that is giving me some problems. what i am trying to do is find every occurrence of a 356 day high. To do this i am trying code similar to the one below, but getting an exception on the "for i" line: 'builtin_function_or_method' object has no attribute 'getitem'
Quote = namedtuple("Quote", "Date Close Volume")
quotes = GetData() # arrray
newHighs = []
for i,q in range[365, len(quotes)]: #<--Exception
max = max(xrange[i-365, i].Close) #<--i know this won't work, will fix when i get here
if (q.Close > max):
newHighs.append(i,q)
Any help on fixing this would be appreciated. Also any tips on implementing this in an efficient manner (since quotes array currently has 17K elements) would also be nice.
range is a function that returns a generator (or list in python2). Thus, it must be called as a function range(365, len(quotes)), which will return all the numbers from 365 to len(quotes).
Square brackets imply indexing, like accessing items in a list. Since range is a function, not a list, it throws an exception when you try to access it.
"Range" is a function. This means you use circle brackets, not square ones. This is the same deal with "xrange" in the line below. I understand why you'd think to use the square brackets, but what "range" does is create the list using those arguments. So it's not the same as when you want elements m to n of a list.
hi im in need of some help for some basic programming exercises im working through. here's the problem.
a) Write a function that returns the maximum value from a list of names. The list of names is passed to the the function, the function returns the 'max' string.(essentially get the name that comes first alphabetically)
b) Write a function that replaces a value in a list with a different one. The parameters of this function are the list, the position in the list of the value that needs to be replaced and the replacement value. Call this function with alist of names in main(). Check if the list was changed by displaying it in main() after the replace function was called. (arrange the list alphabetically.)
in all of this i have to create two main programs, one main program to set up the basics then another seperate function to be called in the main to execute the necessary steps. it needs to be written in python. im completely lost here. heres the list of names im dealing with
bob
Nate
ethel
frank
johnjacobjingleheimerschmidt
clarice
ptolmey
nefertiti
hess
michelle
algernon
For part a), read how to use list.sort(). For part b), read this previous question on how to replace items in a list.
Does len(list) calculate the length of the list every time it is called, or does it return the value of the built-in counter?I have a context where I need to check the length of a list every time through a loop, like:
listData = []
for value in ioread():
if len(listData)>=25:
processlistdata()
clearlistdata()
listData.append(value)
Should I check len(listData) on every iteration, or should I have a counter for the length of the list?
You should probably be aware, if you're worried about this operation's performance, that "lists" in Python are really dynamic arrays. That is, they're not implemented as linked lists, which you generally have to "walk" to compute a length for (unless stored in a header).
Since they already need to store "bookkeeping" information to handle memory allocation, the length is stored too.
Help on built-in function len in module __builtin__:
len(...)
len(object) -> integer
Return the number of items of a sequence or mapping.
so yes, len(list) returns how many items in the list. You might want to describe in more details, providing necessary input files/output to help better understand what you want to do.
len(list) returns the length of a list. If you change it, you'll have to check it's length every iteration. Or use a counter.
len(list) returns the length of the list. Everytime you call it, it will return the length of the list as it currently is. You could set up a counter by taking the len of list initially and then adding 1 to the variable each time something is appended to the list.