Найти - Пользователи
Полная версия: Вывод чисел
Начало » Python для новичков » Вывод чисел
1
AleksandrM
Здравствуйте.
Есть такая задача:
“Список заполняется случайными числами. Затем элементы с четными индексами сортируются в порядке возрастания, а элементы с нечетными индексами сортируются в порядке убывания.”

Вроде получилось, но есть варианты получше ?
Вот мой код:

 listt = [i + 1 for i in range(int(input('введите число: ')))]
for s in listt:
    if s % 2 == 0:
        print(f'{s}', end=',')
print()
for s in reversed(listt):
    if s % 2 != 0:
        print(f'{s}', end=',')
xam1816
 import random as rd
rand_nums = [rd.randint(0,100) for _ in range(10)]
print('начальный список: ', rand_nums, '=>\n')
print('четные индексы: ', rand_nums[::2], '=>\n')
print('нечетные индексы: ', rand_nums[1::2], '=>\n')
#
num_with_even_indexes = sorted(rand_nums[::2])
print('по возрастанию четные', num_with_even_indexes, '=>\n')
#
num_with_odd_indexes = sorted(rand_nums[1::2], reverse=True)
print('по убыванию нечетные', num_with_odd_indexes, '=>\n')
#
sort_rand_nums = [None] * (len(num_with_even_indexes) * 2)
sort_rand_nums[::2] = num_with_even_indexes
sort_rand_nums[1::2] = num_with_odd_indexes
#
print("༼ つ ◕_◕ ༽つ", sort_rand_nums)
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Powered by DjangoBB