Computer Science, asked by anindyaadhikari13, 3 months ago

If you were asked to print the series given below using Python, what would be the shortest códe possible?

1 0 1 0 1 0...10 terms. ​

Answers

Answered by Oreki
18

Program

   print(*[i % 2 for i in range(1, int(input("Enter N - ")) + 1)])

Algorithm

  • Accepting the number of terms.
  • Getting the remainder of each number in range when divided by 2 hence, generating the pattern.
  • Unpacking the list, therefore printing the pattern on the screen.
Attachments:

anindyaadhikari13: You are Genius. Awesome
anindyaadhikari13: Awesome answer.
Oreki: Thanks, I am an Ace though, lol
anindyaadhikari13: Rank doesn't matter, you are genius as always.
BrainlyProgrammer: How did you became 1 line Programmer from 5 line Programmer??
Oreki: Just experience and a few tricks here and there
Oreki: Thank for appreciating!
Answered by BrainlyProgrammer
36

Programming language:-

  • Python

k=1

for i in range(0, 10 + 1):

print(k%2,end=" ")

k += 1

Variable Description:-

1. k:- local variable to print the terms

2. i:- loop variable

Explaination:-

• The Program intialized k=1

• Then it runs i loop from 1 to 10

• Then k%2 where k is divided by 2 and remainder gets printed i.e. either 0 or 1

• k+=1 update the value of k

____

•Output Attached

Attachments:

anindyaadhikari13: Good but not the shortest.
BrainlyProgrammer: yup.. but tried the best
anindyaadhikari13: Good.
Similar questions