Python One-line Python Code that returns a sequence of integers

AI Thread Summary
The discussion focuses on creating a one-line Python generator or iterator that produces a sequence of integers by cumulatively adding the ASCII values of the letters in the word "Close." The initial attempt involves using a for-loop to sum the ASCII values and print the results. However, it is clarified that the loop needs to be repeated, and a cumulative sum can be achieved using NumPy's `cumsum` function. Participants suggest using a list comprehension to generate the ASCII values and then applying `numpy.cumsum` to print the cumulative sums. The final solution confirms that multiplying the list of ASCII values allows for generating the desired sequence, which matches the first ten integers provided in the original post.
mathmari
Gold Member
MHB
Messages
4,984
Reaction score
7
Hey! 😊

I want to write a one-line Python generator or iterator expression that returns the sequence of integers generated by repeatedly adding the ascii values of each letter in the word ā€œCloseā€ to itself. The first 10 integers in this sequence are: 67, 175, 286, 401, 502, 569, 677, 788, 903, 1004. If the code should not be in one line it would be as follows, or not ?
Code:
sum = 0 
for letter in list("Close") : 
    sum += ord(letter) 
    print(sum)
So that we write that in oe line the for-loop will be
Code:
sum += [ord(letter) for letter in list("Close")]
right? But how do we add at this line the print? :unsure:
 
Technology news on Phys.org
mathmari said:
Code:
sum = 0
for letter in list("Close") :
    sum += ord(letter)
    print(sum)
Hey mathmari!

Shouldn't the loop be repeated? (Wondering)

mathmari said:
So that we write that in oe line the for-loop will be
Code:
sum += [ord(letter) for letter in list("Close")]
right? But how do we add at this line the print?
This doesn't parse since we can't add a list to a number. :oops:

I googled how to calculate a cumulative sum in python and found numpy.cumsum.
That is, we can do import numpy; numpy.cumsum([1,2,3]).

We can repeat a list by multiplying it with a number. For instance 5 * [ord(letter) for letter in list("Close")].

We can print a resulting list by calling print before it. For instance print [1,2,3].
šŸ¤”
 
Klaas van Aarsen said:
I googled how to calculate a cumulative sum in python and found numpy.cumsum.
That is, we can do import numpy; numpy.cumsum([1,2,3]).

We can repeat a list by multiplying it with a number. For instance 5 * [ord(letter) for letter in list("Close")].

We can print a resulting list by calling print before it. For instance print [1,2,3].
šŸ¤”

Ahh ! So we should write print(numpy.cumsum([ord(letter) for letter in list("Close") ])) , right? :unsure:
 
mathmari said:
Ahh ! So we should write print(numpy.cumsum([ord(letter) for letter in list("Close") ])) , right?
Yep. (Nod)
And if we add a multiplier before the list, we get:
Python:
>>> print(numpy.cumsum(3 * [ord(letter) for letter in list("Close")]))
[  67  175  286  401  502  569  677  788  903 1004 1071 1179 1290 1405 1506]
It matches the first 10 integers in the opening post. šŸ¤”
 
Klaas van Aarsen said:
Yep. (Nod)
And if we add a multiplier before the list, we get:
Python:
>>> print(numpy.cumsum(3 * [ord(letter) for letter in list("Close")]))
[  67  175  286  401  502  569  677  788  903 1004 1071 1179 1290 1405 1506]
It matches the first 10 integers in the opening post. šŸ¤”

Ah ok! I got it! Thank you very much! :geek:
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...

Similar threads

Replies
3
Views
1K
Replies
11
Views
1K
Replies
8
Views
2K
Replies
23
Views
2K
Replies
13
Views
2K
Replies
8
Views
2K
Replies
6
Views
3K
Replies
2
Views
2K
Replies
8
Views
2K
Back
Top