for (int i = 0; i < n; ++i) { int ci; scanf("%d", &ci); int mn = INF, mx = 0; for (int j = 0; j < ci; ++j) { int x; scanf("%d", &x); mx = max(mx, x); mn = min(mn, x); } a[i] = make_pair(mn, mx); } 

It is required to implement a similar code on python. Those. only the maximum and minimum element in the string is required. But at the same time, the program execution time does not allow writing everything to the list and finding the minimum and maximum in it. Example line 1 2 3 4 at the input and you need to find 1 and 4. How to implement it?

    3 answers 3

     digits = [0,0] input_digits = input('Enter digits with comma (,) separator :').split(',') for digit in input_digits: if int(digits[0]) > int(digit): digits[0] = digit if int(digits[1]) < int(digit): digits[1] = digit print(digits); 

    Sandbox result
    Sandbox

    • Of course, it could be done with dictionaries - but did not bother. This answer is working. You can test with sand. - Roman Kozin
    • will this code work faster than for example: a = list (map (int, input (). split ())) max (a) - Denzila
    • @Denzila; Memory allocation is completely different. I can not say exactly how much faster / slower it will work. - Roman Kozin
    • In the example you cited, the string is read character-by-character or in its entirety? - Denzila
    • is it possible in python to somehow read a string separated by spaces, character-by-character? - Denzila
     st = '2 1 3 41 7 5 6' gs = map(int, st.split()) # generator mn = mx = next(gs) # min=max=первый_элемент(int) for s in gs: if mn > s: mn = s if mx < s: mx = s print(mn, mx) 

      To find the minimum and maximum, bypassing an arbitrarily large (but finite) collection of numbers only once (single-pass algorithm):

       def minmax(iterable, *, default=None): it = iter(iterable) min_ = max_ = next(it, default) for item in it: if min_ > item: min_ = item if max_ < item: max_ = item return min_, max_ 

      To read numbers separated by a space from standard input or files specified from the command line:

       #!/usr/bin/env python """Usage: minmax [<file>...]""" import fileinput print(*minmax(n for line in fileinput.input() for n in map(int, line.split()))) 

      What * means see here .