-7

NOTE: This is for a homework assignment, but the portion I have a question on is ok to ask help for.

I have to script out a sequence 11110000111000110010 (i am using python) without using switches or if statements and only a maximum of 5 for and whiles.

I already have my script laid out to iterate, I just can't figure out the algorithm as recursive or explicit let alone whether the element's are 1's 2's or 4's =/

As much as we have learned so far there is no equation or algorithm to use to figure OUT the algorithm for sequence. Just a set of instructions for defining one once we figure it out. Does anyone see a pattern here I am missing?

EDIT: What I am looking for is the algorithm to determine the sequence. IE the sequence 1,3,6,10,15 would come out to be a[n]=(a[n-1]+n) where n is the index of the sequence. This would be a recursive sequence because it relies on a previous element's value or index. In this case a[n-1] refers to the previous index's value. Another sequence would be 2, 4, 6, 8 would come out to be a[n] = (n*2) which is an explicit sequence because you only require the current index or value.

EDIT: Figured it out thanks to all the helpful people that replied.... I can't believe I didn't see it =/

6 Answers6

3

Note how there's a nested structure here. In pseudocode (so you do the python yourself):

for i in 4 .. 1:
    for b in 1 .. 0:
         for j in 1 .. i:
            print b
Ami Tavory
  • 74,578
  • 11
  • 141
  • 185
3

There are many possible solutions to this problem. Here's a reusable solution that simply decrements from 4 to 1 and adds the expected number of 1's and 0's.

Loops used : 1

def sequence(n):
    string = ""
    for i in range(n):
        string+='1'*(n-i)
        string+='0'*(n-i)
    return string

print sequence(4)

There's another single-line elegant and more pythonic way to do this:

print ''.join(['1'*x+'0'*x for x in range(4,0,-1)]) 

Loops used : 1, Lines of code : 1

;)

Aditya
  • 3,080
  • 24
  • 47
  • 1
    +1 for the one-liner. In principle, I dislike clever on-liners because they are often overly complex and obscure, but I agree, this is elegant and pythonic. Edit: Although... We really shouldn't be writing this guy's code for him. – SiHa Jun 11 '15 at 06:48
  • Hmmm well right point. We're doing homework and not getting any grades for it. – Aditya Jun 11 '15 at 06:56
  • 1
    I'm sorry if it seemed that way. =/ I was mostly looking for another pair of eyes on the sequence to see if anyone could spot an algorithm, not for a full python solution. I should have been more clear. I appreciate the elegant code but It was not my intention to have anyone do my assignments for me and I apologize if I misrepresented this. It was just a small part of an assignment that had a tricky discrete section I overthought =) – Jake McBride Jun 11 '15 at 09:48
  • @JakeMcBride no problem. Well most active users on stack overflow are quite strict about is because there are too many kids asking homework assignments - and best part is, those who answer are not actually learning much from it. Though you added an EDIT much later, the original question didn't seem like part of something bigger. Those who downvoted the question couldn't find where is such a sequence required to be printed if not as an homework assignment. – Aditya Jun 11 '15 at 13:57
2

You could try this:

print ''.join(['1'*i+'0'*i for i in range(4,0,-1)]) 
lana
  • 21
  • 3
  • 2
    But make sure you fully understand the code you're using, especially if it's homework. It exist to help you learn! – lana Jun 15 '15 at 16:48
0
b_len = 4
ones = '1111'
zeros = '0000'
s = ''
for n in range(b_len, -1, -1):
    s = s + ones[:n] + zeros[:n]
print s 

prints:

11110000111000110010
Scott
  • 6,089
  • 4
  • 34
  • 51
0

I see. Four "1" - four "0", three "1" - three "0", two "1" - two "0", one "1" - one "0". 20 digits in total. What it means I have no clue.

#!/usr/bin/python

s=''

i=4
while i >0:
    s=s+'1'*i+'0'*i
    i -=1

print s

11110000111000110010

Alex Ivanov
  • 695
  • 4
  • 6
0

Is it exactly this sequence or do you want to be abble to change the length of the 1st sequence of 1?

you can use a reversed iteration loop like in this code:

def askedseq(max1):
   seq = [] # declaring temporary sequence
   for i in range(max1,0,-1): # decreasing iteration loop
      seq += i*[1] + i*[0] # adding the correctly sized subseq
   return seq

print askedseq(4) #prints the required sequence
print askedseq(5) #prints the equivalent sequence with 11111

prints: 11110000111000110010

111110000011110000111000110010

you can also look at numpy to do such things

aTben0
  • 314
  • 1
  • 6