0

The input should be a series of pairs of integers with one pair of integers per line. Each pair forms a range that is used to make several calculations and outputs a single integer, along with the two initial integers. The two integers in the input should be separated by a single space. The following code only takes a single line of input.

What changes should I make to have it take multiple lines of input and print out multiple lines of output? The code is written in python3

i,j = input().split(" ")
max_count = 0
n = int(i)
if int(i) >= int(j):
    for n in range(int(j), int(i)):
            count = 2
            while n > 1:
            if count > max_count:
                    max_count = count
            if n == 1:
                    count = 1
            elif n % 2 != 0:
                    n = (3 * n) + 1
                    count += 1
            if n % 2 == 0:
                    n = n / 2
                    count += 1
if j > i:
    for n in range(int(i), int(j)):
            count = 2
            while n > 1:
            if count > max_count:
                    max_count = count
            if n == 1:
                    count = 1
            elif n % 2 != 0:
                    n = (3 * n) + 1
                    count += 1
            if n % 2 == 0:
                    n = n / 2
                    count += 1        
print(i, j, max_count)

Here is a link to the problem I’m attempting to solve: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=29&page=show_problem&problem=36

Logan Kroeze
  • 85
  • 1
  • 10

2 Answers2

0

From How to get multiline input from user:

no_of_lines = 5
lines = ""
for i in range(no_of_lines):
    lines+=input()+"\n"

print(lines)

You can set the number of lines of input to whatever is necessary for you.

lovinghate
  • 63
  • 7
0

This is what I understood:

Your code is working fine, but only takes one line (or one pair) for input. You need a method to input more than one line (or one pair).


Create a text file named "input.txt" within the same directory that your python file is stored in. Enter all of the pairs in there, one per line, and save the file.

Modify your code as such:

# Opens the text file and imports the sets into a list
with open('input.txt') as f:
    input_list = f.read().splitlines()

for pair in input_list:
    # Splits the set
    i, j = pair.split(' ')

    # Put the rest of your code here just as it was ...
    max_count = 0
    n = int(i)
    # And so on ...
Brice Frisco
  • 409
  • 1
  • 4
  • 15
  • Your understanding is 100% correct, but I’m only able to upload the .py file by itself. Any way to do this without specifying a certain number of lines or using a .txt file? – Logan Kroeze Sep 16 '18 at 03:31