0

I have this code here and I'm looking for a way to check if min_score and max_score changes, and count how many times it changes. I can't seem to find a way to do it:

games = int(input())
score = list(input().split())
score = [int(x) for x in score]
for y in range(1, len(score) + 1):
    min_score = (str(min(score[:y])) + " MIN")
    max_score = (str(max(score[:y])) + " MAX")
    print(min_score)
    print(max_score)

This is a sample test case for reference:

9
10 5 20 20 4 5 2 25 1

First number is the size of the array, which in my code I never use because I make an array just from the string of numbers below ( in fact I don't even know why they give the size). Basically I need to find how many times the max and min values change. I'm still a beginner in programming and I don't really know what to do..

5
  • You should save the previous value of min/max and compare. did i understand right? you want to see if it changes in current iteration? Commented Feb 9, 2021 at 9:55
  • Please do not edit solution announcements into the question. Accept (i.e. click the "tick" next to it) one of the existing answer, if there are any. You can also create your own answer, and even accept it, if your solution is not yet covered by an existing answer. Commented Feb 9, 2021 at 10:12
  • It won’t let me accept my own answer.. Commented Feb 9, 2021 at 11:03
  • Weird, as far as I know it should, at least after a few minutes. Please try again. Otherwise you do have the privilege to ask about this in meta. meta.stackoverflow.com Commented Feb 9, 2021 at 13:46
  • I can use my own answer in 2 days... it says Commented Feb 9, 2021 at 18:44

4 Answers 4

2

you could just keep track of the lowest and highest number encountered and check if the current score is just below or above. A simple script could look like this:

scores = [10,5,20,20,4,5,2,25,1]

countChanges = 0
limitLow  = float("inf")
limitHigh = -float("inf")

for s in scores:
  if(s < limitLow):
    countChanges += 1
    limitLow = s
  if(s > limitHigh):
    countChanges += 1
    limitHigh = s
  
  print("current score: %3d   limits: [%2d .. %2d]   changes:%d" % (s, limitLow, limitHigh, countChanges))
Sign up to request clarification or add additional context in comments.

Comments

0
spam = [10, 5, 20, 20, 4, 5, 2, 25, 1]
print(sum(n < min(spam[:idx]) for idx, n in enumerate(spam[1:], start=1)))
print(sum(n > max(spam[:idx]) for idx, n in enumerate(spam[1:], start=1)))

output

4
2

if you also want to account for initial value - add 1.

Comments

0

Looks like a hankerrank problem? They often give the length of the input to allow solutions without knowing about built-in methods like len().

Anyway, You can initialise min and max to the first element of the array (if it exists, check the specification), or some suitably small and large values (again, check the possible values).

Then you can count the number of times min and max changes as you go.

Should be easy enough to adapt for the case that you just want to track any change, not min and max separately. It wasn't clear from your question.

scores = [10, 5, 20, 20, 4, 5, 2, 25, 1]
min_score = scores[0]
max_score = scores[0]
min_score_changes = 0
max_score_changes = 0
for score in scores:
  if score < min_score:
    min_score = score
    min_score_changes += 1
  if score > max_score:
    max_score = score
    max_score_changes += 1

Comments

0

I solved it like this after some thinking:

games = int(input())
score = list(input().split())
score = [int(x) for x in score]
min_score_list, max_score_list = [] , []
for y in range(1, len(score) + 1):
    min_score = (min(score[:y]))
    max_score = (max(score[:y]))
    if min_score not in min_score_list:
        min_score_list.append(min_score)
    if max_score not in max_score_list:
        max_score_list.append(max_score)
print((len(max_score_list) - 1), len(min_score_list) - 1)

I know it's not perfect code but at least I did myself :D

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.