0

Right, so this might be a rather confusing question. But in my Computer science class, we were given this optional challenge to calculate the distances a catapult has achieved after a user inputs a set of angles and speeds. But the challenge is that we have to split the problem into multiple smaller functions but each function is only allowed to have one statement per function.

I think I have a way to do it but I'm having trouble with one part.

[x = get_angles(),  y = get_speeds(): 2*x[i]*y[i]/GRAVITY for i in range(len(x))]

This is part of my code for creating a list of the distances travelled. Now, this is effectively pseudo-code cause I have no clue how to make it work. Does python have anything that allows something like this to happen?

Any help would be great. Sorry for being so long-winded but thanks anyway :)

Flair
  • 2,123
  • 1
  • 24
  • 39
MoosyLager
  • 25
  • 3
  • 2
    is that _supposed_ to be a list comprehension? or a function definition? – Matiiss Mar 21 '22 at 20:54
  • 3
    I'm not really following your explanation. Could you clarify what your problem statement is, and what the restrictions are? (a bulleted list as well as some concrete function signatures would be very nice here) – BrokenBenchmark Mar 21 '22 at 20:57
  • 1
    I **think** the question is: "I have a function named `get_angles` and a function named `get_speeds`, which will each return a list, and those lists will be the same length. How can I write a single statement, the produces a list, where each element results from applying a mathematical function to a pair of values, where the pairs of values are taken pairwise from the lists returned by those other functions?" – Karl Knechtel Mar 21 '22 at 21:06
  • 1
    In which case, there are two underlying questions here: 1) "How do I apply a function repeatedly to a sequence of values?", and "how do I pair up values from two source sequences?". Both of these are common questions which I can link as duplicates, pending confirmation that I correctly understand the problem. – Karl Knechtel Mar 21 '22 at 21:08
  • @KarlKnechtel the problem as stated in the title is using function return values in the same line as the call to the functions. – Ender_The_Xenocide Mar 21 '22 at 21:09
  • 2
    (That said: "Sorry for being so long-winded" The only thing "unnecessary" here is that line itself - this is *not a discussion forum*, so we don't want commentary like that (or "any help would be great"; of course we understand that implicitly). The problem with this question is that it is *vague*. "I have no clue how to make it work" doesn't tell us what "it" is, and "Does python have anything that allows something like this to happen?" doesn't tell us what "this" is. – Karl Knechtel Mar 21 '22 at 21:10
  • @KarlKnechtel Righto. Yeah sorry for the poorly worded question, but thank you for the clarifications to my post – MoosyLager Mar 21 '22 at 21:16
  • This shouldn't have been closed. This isn't a duplicate because it's asking to do everything in a single line. This question is NOT answered by the "duplicate" posts that were marked. – Ender_The_Xenocide Mar 21 '22 at 21:17

1 Answers1

0

Trying to change the line of code you provided into something that works, I got this:

(lambda x, y: [2*x[i]*y[i]/GRAVITY for i in range(len(x))])(get_angles(), get_speeds())

This trick uses a lambda function to "store" the x, y values and use them in the same line.

I'm not sure this does exactly what you want, but this lambda function trick still applies.