One-line Python Code that returns a sequence of integers

Click For Summary

Discussion Overview

The discussion revolves around creating a one-line Python generator or iterator expression that produces a sequence of integers derived from the ASCII values of the letters in the word "Close." Participants explore methods to achieve cumulative sums and the use of libraries like NumPy for this purpose.

Discussion Character

  • Exploratory
  • Technical explanation
  • Mathematical reasoning

Main Points Raised

  • One participant proposes a method to generate a sequence by summing the ASCII values of the letters in "Close" and suggests a one-line version of the for-loop.
  • Another participant questions the need for repetition in the loop and points out that adding a list to a number does not parse correctly.
  • There is mention of using NumPy's cumulative sum function, with examples provided for its application.
  • A suggestion is made to print the cumulative sum directly using NumPy, which is acknowledged by others in the thread.
  • Participants discuss the effect of multiplying the list of ASCII values to generate a longer sequence of integers, confirming it matches the initial sequence provided.

Areas of Agreement / Disagreement

Participants generally agree on the use of NumPy for calculating cumulative sums and the method of multiplying the list to extend the sequence. However, there is some uncertainty regarding the initial approach to creating the one-liner and whether repetition in the loop is necessary.

Contextual Notes

Some assumptions about the use of libraries and the handling of lists in Python are present, but not all participants clarify their understanding of these concepts, leading to potential gaps in the discussion.

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:
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 11 ·
Replies
11
Views
1K
  • · Replies 34 ·
2
Replies
34
Views
5K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 23 ·
Replies
23
Views
2K
  • · Replies 13 ·
Replies
13
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K