#🔒 Finding extreme distances in an integer sequence using NumPy (i've never done this type of exercis

47 messages · Page 1 of 1 (latest)

silent tapir
#

Write a program to input a sequence of n integers from the keyboard. The distance between two numbers is defined as the distance between the two points representing them on a number line. Print to the screen:

The two numbers with the greatest distance and their distance.

The two consecutive numbers in the sequence with the smallest distance and their distance.

(Note: If there are multiple pairs that satisfy the condition, print the first pair based on their order in the sequence).

signal vectorBOT
#

@silent tapir

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

compact latch
#

What do you need help with?

silent tapir
#

i need an idea for smallest and pair

#

and how to use numpy in this exercise

compact latch
#

so let’s say you input 1,2,7

#

the smallest pair is (1,2) and the largest is (1,7)

#

how do you find that out

silent tapir
#

I take 1 and add them together.

If 1 + 2 < 1 + 7, then 1, 2 is the smallest number. i think so :))

compact latch
#

what about for -5,1,2

#

what’s the smallest pair

silent tapir
#

-5 and 1

compact latch
#

think about it. Is that the pair with the smallest distance?

#

think of a number line like the question asks

silent tapir
#

distance is always possitive numbers

compact latch
#

right

#

so what’s the smallest pair

#

out of -5,1,2

silent tapir
#

1 and 2

#

right?

compact latch
#

Yes

#

Correct

#

So now if I gave you 10 different numbers

#

Can you think of an algorithm that finds the smallest and largest pair

#

How would you do it?

#

Don’t think about code right now just describe the process

silent tapir
#

I think I converted them all to positive numbers using the abs function, and then find a smallest number

compact latch
#

Ur getting there

#

But you don’t want to convert everything to positive

#

You only want to convert distance positive

#

There’s a formula to find the distance between two numbers

#

dist(x,y) = | x - y |

#

where | | is absolute

silent tapir
#

i see

compact latch
#

with that you should be able to implement some code

dire bone
#

are the numbers always going to be entered from smallest to largest?

silent tapir
#

which much better ? numpy or math

compact latch
#

numpy works well here

dire bone
#

I don't think you need numpy for this. You can use a list for this as well.

compact latch
#

I think numpy may be part of the assignment

silent tapir
#

brb

compact latch
#

numpy can also be vectorized which is nice

dire bone
#

The largest distance is obvious. I can't think of a faster than O(N) algo for the shortest

white tartan
#

hint:

[1, 5, 3, 4, 2]
```assume the above is `a`, then `a[1:]` and `a[:-1]` respectively are

[5, 3, 4, 2]
[1, 5, 3, 4]

signal vectorBOT
#
Python help channel closed for inactivity

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.