Using Python! Thanks 4. (First printings, 15pt) If you\'ve ever looked at the co
ID: 3875580 • Letter: U
Question
Using Python! Thanks
4. (First printings, 15pt) If you've ever looked at the copyright of a book (remember, heavy thingy with pages you had to turn?) you may have seen number sequences like 13 579 108 642 What this means is that the book is a first printing (of a first edition, for example). When the publisher reprints the book, they just drop the 1, and the smallest number, 2 at the end, tells you that you have a second printing, and so on. Why order the numbers in this alternating fashion? In this way, the number sequence remains nicely centered (this made more sense when typesetting stil literally meant that type was being set, but the custom remained). Write a program alternate(n) which creates and returns a string containing the numbers from 1 through n in this alternating order. Below are some test-runs. There are two restrictions: do not use slicing (so no s1::2] allowed), though indexing is fine (si], etc.); and only use a single loop which adds one number in each iteration. Below are two hints if you want partial spoilers. Python 3.5.2 Shell Eile Edit Shell Debug Options Window Help >>>alternate (5) 1 3 5 4 2' >>>alternate (6) '1 3 5 6 4 2 >>>alternate (10) '1 3 5 7 9 10 8 6 4 2 >alternate (20) '1 3 5 7 9 11 13 15 17 19 20 18 16 14 12 10 8 6 4 2 Ln: 21 Col: 13Explanation / Answer
Python Program for alternate function
Program:-
# Function
def alternate(number):
str1=""
str2=""
str3=""
for i in range(1,number+1): #for loop for odd or even splitting
if i%2!=0:
str1+=str(i)
elif i%2==0:
str2+=str(i)
str3=reversed(str2)
str1=" ".join( str1) #join space between numbers
str3=" ".join(str3)
print(str1+" "+str3) # Display the numbers
number=input("Enter alteration number : ") #user can input alternate number
alternate(number) #Call the function
---------------------------------------------------------------------------------------------------------------------------------
Output
Python 2.7.14 (v2.7.14:84471935ed, Sep 16 2017, 20:19:30) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>>
=================== RESTART: C:/Python27/stringexample.py ===================
Enter alteration number : 5
1 3 5 4 2
>>>
=================== RESTART: C:/Python27/stringexample.py ===================
Enter alteration number : 6
1 3 5 6 4 2
>>>
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.