Python Forum
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
number problem
#1
Hi,

How to write a program which reads integers from the standard input (until it gets a negative number) and puts them into an array. After that it calls processArray on the array, and then prints the value that is returned to standard output.


Currently, processArray does not do anything useful - it just returns 0.

we have to change processArray so that it finds the longest losing streak in the array, and returns the loss of the streak.

A sequence of consecutive numbers in which each number is less than or equal to the previous number is called a losing streak. The difference in value between the last number of the streak and the first number of the streak is known as the loss.

For example, if these numbers were provided on the standard input:


3
6
36
32
32
121
66
24
22
371
661
6
4
8
-1

The losing streaks in this sequence are 36, 32, 32, and 121,66,24,22, and 661,6,4, and the corresponding losses are 4 and 99 and 657. In this case, the 2nd streak with 4 numbers is the longest, so your program should print it's loss:

99

we need to make sure that program prints a single integer to the standard output and nothing else. there should not be any Any unnecessary printf/println/putchar etc.
Reply
#2
What have you tried?
Reply
#3
Hardcode that list until you get the function working. I would not want to type that in over and over and over and over...
Reply
#4
Quote:In the face of ambiguity, refuse the temptation to guess.

Is datastructure required ('puts them into an array') really Python array or list?
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#5
sequence = True
all_entered_numbers = [0]
win_list = []
loss_list = []
high_num = 0
low_num = 0
loss = []

while sequence:
    current_num = int(input("enter a number: "))
    if current_num >= 0:
        all_entered_numbers.append(current_num)
        prev_num = all_entered_numbers[-2]
        if current_num > prev_num:
            win_list.append(current_num)
            loss_list.append(0)
            high_num = current_num
            loss.append(0)
        elif current_num < prev_num:
            loss_list.append(current_num)
            win_list.append(0)
            low_num = current_num
            loss.append(high_num - low_num)
        else:
            pass
    else:
        sequence = False

print(f"all entry are : {all_entered_numbers}")
print(f"winning list is : {win_list}")
print(f"losing list is : {loss_list}")
print(f"loss occurence is : {loss}")
Output:
enter a number: 3 enter a number: 6 enter a number: 36 enter a number: 32 enter a number: 32 enter a number: 121 enter a number: 66 enter a number: 24 enter a number: 22 enter a number: 371 enter a number: 661 enter a number: 6 enter a number: 4 enter a number: 8 enter a number: -1 all entry are : [0, 3, 6, 36, 32, 32, 121, 66, 24, 22, 371, 661, 6, 4, 8] winning list is : [3, 6, 36, 0, 121, 0, 0, 0, 371, 661, 0, 0, 8] losing list is : [0, 0, 0, 32, 0, 66, 24, 22, 0, 0, 6, 4, 0] loss occurance is : [0, 0, 0, 4, 0, 55, 97, 99, 0, 0, 655, 657, 0] Process finished with exit code 0
I'm novice still I tried my best to solve your problem partially.
As you can see in output the list named "loss occurrence" , you can see longest loss sequence is second one with loss of '99' finally.
It's up to you how you wanna get it displayed.

Sorry if you already know this and I have wasted your time.
Professional Dentist(32years) fell in love with Python during COVID-19 Lockdown.

"Nothing can stop you from learning new things except your own will"

Reply
#6
here is final solution.
I have added few more code line to my previous post to arrive at solution.

sequence = True
all_entered_numbers = [0]
win_list = []
loss_list = []
high_num = 0
low_num = 0
loss = []

while sequence:
    current_num = int(input("enter a number: "))
    if current_num >= 0:
        all_entered_numbers.append(current_num)
        prev_num = all_entered_numbers[-2]
        if current_num > prev_num:
            win_list.append(current_num)
            loss_list.append(0)
            high_num = current_num
            loss.append(0)
        elif current_num < prev_num:
            loss_list.append(current_num)
            win_list.append(0)
            low_num = current_num
            loss.append(high_num - low_num)
        else:
            pass
    else:
        sequence = False

print(f"all entry are : {all_entered_numbers}")
print(f"winning list is : {win_list}")
print(f"losing list is : {loss_list}")
print(f"loss occurence is : {loss}")

all_instances = []  # makes nested list of loss occurence
for i in loss:
    if i == 0:
        all_instances.append([])
    else:
        all_instances[-1].append(i)
loss_instances = list(filter(None, all_instances))  # removing zero values
print(f'loss instance = {loss_instances}')
longest = [len(i) for i in loss_instances]  # to find out longest sequel of loss
most_loss = loss_instances[longest.index(max(longest))][-1]
print(f"Most loss is : {most_loss}")
print(most_loss)
Output:
enter a number: 3 enter a number: 6 enter a number: 36 enter a number: 32 enter a number: 32 enter a number: 121 enter a number: 66 enter a number: 24 enter a number: 22 enter a number: 371 enter a number: 661 enter a number: 6 enter a number: 4 enter a number: 8 enter a number: -1 all entry are : [0, 3, 6, 36, 32, 32, 121, 66, 24, 22, 371, 661, 6, 4, 8] winning list is : [3, 6, 36, 0, 121, 0, 0, 0, 371, 661, 0, 0, 8] losing list is : [0, 0, 0, 32, 0, 66, 24, 22, 0, 0, 6, 4, 0] loss occurence is : [0, 0, 0, 4, 0, 55, 97, 99, 0, 0, 655, 657, 0] loss instance = [[4], [55, 97, 99], [655, 657]] Most loss is : 99 99
you can remove some print function lines to remove extra output. I kept to figure out what is happenning at those lines.
Professional Dentist(32years) fell in love with Python during COVID-19 Lockdown.

"Nothing can stop you from learning new things except your own will"

Reply
#7
upon executing above program on python 3 platform getting below errors:-

Sorry. Your solution is incorrect. Please try again. Feedback for your solution:

The function does not work as expected. Please check the following carefully:

Please ensure that the program produces the expected output as given in the problem
Make sure your answer contains a full, working program.
Make sure your processArray function/method is returning the appropriate value
If you wrote a C program, make sure that the program is not using any functions not found in the standard C library. Specifically, it should not use any Microsoft or Turbo C specific functions like getch or clrscr or conio.h
Make sure that the program is not printing anything extra to the output
Make sure that the program is not trying to read any input. No scanf or getc
Reply
#8
As usual, we aren't here to do your work for you. Probably the person above who gave you the code didn't know that, but you've been here long enough by now. Write code for yourself and try to debug it when it doesn't work as you expect. Just expecting to be spoonfed isn't going to help you become a decent programmer.
Reply
#9
I do agree but when problem is so tough and when trying to execute it and when it is giving so many errors then what to do?

it's not i am not trying,i have been struggling a lot to resolve these errors and unable to overcome these tough errors so looking experts guidance here.

Thanks..
Reply
#10
You've just taken code that someone else wrote for you and that doesn't conform to the requirements. Why don't you start by

1. Reading the requirements
2. Working out how to break the task down into small steps
3. Working out how to implement each of those small steps

You've been given some example data to work with, so as mentioned above, why don't you hard code it in your program and use that to get you going? This is pretty much what I'd do in the real world - take my example, write a test case including it and work on getting that to pass (I'm not suggesting you write tests here, because that might be out of scope for you right now, but I'm more saying it to illustrate how we work).

Also, if you're unable to even break down relatively simple problems like this, I have no idea how you're going to try and build a web application, which is a much more complex problem.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Problem with number of rows containing null values PythonSpeaker 3 2,239 Nov-23-2019, 06:53 PM
Last Post: ibreeden

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020