-
Notifications
You must be signed in to change notification settings - Fork 0
/
W10_PA3.py
92 lines (63 loc) · 2.16 KB
/
W10_PA3.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# Given a list A of elements of length N, ranging from 1 to N.
# All elements may not be present in the array.
# If the element is not present then there will be -1 present in the array.
# Rearrange the array such that A[i] = i and if i is not present display -1 at that place.
# Input Format:
# The first line contains n numbers with each number separated by a space.
# Output Format:
# Print the elements of the list after the modification.
# Example:
# Input:
# -1 -1 6 1 9 3 2 -1 4 -1
# Output:
# -1 1 2 3 4 -1 6 -1 -1 9
# Explanation:
# The modified list contains elements such that A[i] = i
# --------------------------------------------------------------------------------------------
# Take input in list.
lst = list(map(int, input().split()))[:]
# Sort the list so all -1 are in begining.
# Note that Number of -1 in list = number of missing elements.
lst.sort()
# For swapping.
temp = 0
# i is basicallly the number starting from 0.(whole numbers)
for i in range(0, len(lst)):
# If i = j(any other elements in list) then swap.
for j in range(0, len(lst)):
if i == lst[j]:
# Swapping.
temp = lst[i]
lst[i] = lst[j]
lst[j] = temp
# Printing the required output.
for i in range(0, len(lst)):
if i < (len(lst)-1):
print(lst[i], end=" ")
if i == (len(lst)-1):
print(lst[i], end="")
if (len(lst)) == 0:
print("-1", end="")
# Sort the list in such a way that all -1 in list are at last.
# And all +ve number are from 0 to n (increasing order).
# def count_element(lst):
# count = 0
# for i in range(0, len(lst)):
# if lst[i] == -1:
# count += 1
# return count
# res = count_element(lst)
# print(res)
# def suffix_element(list):
# j = len(lst) - 1
# temp = 0
# lst.sort()
# for i in range(0, len(list)):
# if list[i] == -1 and i < j:
# print(i)
# temp = lst[i]
# lst[i] = lst[j]
# lst[j] = temp
# j = j - 1
# return lst
# print(suffix_element(lst))