I can't figure out this sequence - 11110000111000110010

539 Views Asked by At

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

There are 6 best solutions below

4
On BEST ANSWER

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

;)

1
On

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

2
On

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
2
On
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
0
On

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

1
On

You could try this:

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