H
H
Hikikomori912015-09-21 15:07:09
Python
Hikikomori91, 2015-09-21 15:07:09

Is there an alternative to the join method?

Swap adjacent list elements (A[0] c A[1], A[2] c A[3], etc.). If there are an odd number of elements, then the last element remains in its place.

x = [int(i) for i in input().split()]
k = int(input())
z = []
i = 0
for elem in (x):
    if i != k:
        z.append(elem)
        print(' '.join(z))
    i+=1

All elements in the list are different. Swap the minimum and maximum element of this list.
x = [int(i) for i in input().split()]
num1 = 0
num2 = 1
num3 = 2
for i in range(0, len(x)):
    if x[i] > num1:
        num1 = x[i]
    num2 = x[i]
    if num2 < num3:
        num3 = num2
for i in range(0, len(x)):
    if x[i] == num1:
        x[i] = num3
    elif x[i] == num3:
        x[i] = num1
    print(' '.join(x))

Swap adjacent list elements (A[0] c A[1], A[2] c A[3], etc.). If there are an odd number of elements, then the last element remains in its place.
x = [int(i) for i in input().split()]
x1 = 0
x2 = 0
for i in range (1, len(x), 2):
    x1 = x[i]
    x2 = x[i-1]
    x[i] = x2
    x[i-1] = x1
print(' '.join(x))

So here are 3 puzzles and my terrible solutions. The fact is that the join method completely refuses to work in the visualizer with which I am forced to work. Tell me, is it possible to somehow rewrite all this code so that the array becomes a string without the join method?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
tsarevfs, 2015-09-21
@Hikikomori91

join wants an array of strings. ' '.join([str(a) for a in your_int_array])

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question