Skip to content

Repeats and order

Read · 1 of 3

Shortcuts that lose information

A set removes repeats, but it also forgets the order things came in. Comparing only the first and last items forgets everything in between.

When code takes a shortcut, ask what information the shortcut throws away.

Read this lesson as text

Code that forgets about repeats, or about order.

Shortcuts that lose information

A set removes repeats, but it also forgets the order things came in. Comparing only the first and last items forgets everything in between.

When code takes a shortcut, ask what information the shortcut throws away.

Break it: Break the de-duplicator

This should print the numbers with repeats removed, keeping the order they first appeared in. Find an input where it gets the order wrong.

n = int(input())
nums = list(map(int, input().split()))
print(*list(set(nums)))

Input rules: first line n (1 to 20), second line n whole numbers between 0 and 99.

Break it: Break the sorted check

This should print YES if the numbers never go down from left to right, otherwise NO.

n = int(input())
nums = list(map(int, input().split()))
print("YES" if nums[0] <= nums[-1] else "NO")

Input rules: first line n (1 to 20), second line n whole numbers between -1000 and 1000.