-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunction.py
More file actions
78 lines (48 loc) · 1.49 KB
/
function.py
File metadata and controls
78 lines (48 loc) · 1.49 KB
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
'''
🧾 The Code:
python
Copy
Edit
n = int(input())
result = ''.join(map(str, range(1, n + 1)))
print(result)
🧠 Explanation:
1. n = int(input())
This line takes user input (which is a string), converts it to an integer, and stores it in the variable n.
For example, if the user types 5, then n = 5.
2. range(1, n + 1)
range(1, n + 1) creates a list of numbers starting from 1 up to and including n.
Example: If n = 5, this creates: 1, 2, 3, 4, 5
3. map(str, range(1, n + 1))
map(str, ...) converts each integer in the range to a string.
Example: ["1", "2", "3", "4", "5"]
4. ''.join(...)
This joins all the strings together into one long string with no space between them.
The '' means "join with an empty string".
Example: '1' + '2' + '3' + '4' + '5' = '12345'
5. print(result)
This prints the final string to the screen.
🔄 In Summary:
You're:
Getting a number from the user
Generating a list of numbers from 1 to that number
Turning those numbers into strings
Joining them all together
Printing the final string
'''
'''
The included code stub will read an integer, , from STDIN.
Without using any string methods, try to print the following:
Note that "" represents the consecutive values in between.
Example
Print the string .
Input Format
The first line contains an integer .
Constraints
Output Format
Print the list of integers from through as a string, without spaces.
'''
if __name__ == '__main__':
n = int(input())
result = ''.join(map(str,range(1, n + 1)))
print(result)