## [Problem] Exercise in python

This is the place for queries that don't fit in any of the other categories.

### [Problem] Exercise in python

Hello,

I am a beginner in python and I have some difficulties with this language.
I have not been able to solve an exercise. That's what I do:
Code: Select all
`> A: 2> B: 4> N: 10> SEQ: 0101001001000100011110110000101001100111100001001001111001000000023: 0015: 10 0112: 10011: 001 000 1110: 0108: 01007: 1001 00106: 0000 1115: 1000 110 0114: 1100 0011 0001`

I have to detect, in a sequence of data bits, the patterns having a length between A and B (included) that are repeated more often.
The tool needed to get the highest frequency patterns N (number of occurrences) appearing in different sequence.

I have to count the number of occurrence of each pattern contained in a sequence of bits.
A is the minimum length of the pattern
B is the maximum length of pattern
N is the number of distinct frequency patterns for search
As a result, the Python script to display a list of N groups of motifs found. every line should be in the format: frequency: pattern ... pattern, where the frequency is the
number of occurrences of patterns. The lines should appear in descending order depending on the frequency patterns, and or within the same line, the patterns must appear in
the descending order of length. Patterns of the same length must be entered in order decreasing their numeric value. If there are less than N distinct frequencies, the output list can be less than N lines.

I can only use conditional statements, loops, functions, strings, tuples and lists.

Sorry for my english (google translate)
Last edited by micseydel on Sat Nov 02, 2013 12:28 am, edited 1 time in total.
Reason: First post lock.
Kopp92i

Posts: 4
Joined: Fri Nov 01, 2013 11:50 pm

### Re: [Problem] Exercise in python

And thanks for mentioning that you're not a native English speaker, you should get plenty of patience with your English. What language do you prefer though? There's a chance someone here might know it.
Due to the reasons discussed here we will be moving to python-forum.io on October 1, 2016.

This forum will be locked down and no one will be able to post/edit/create threads, etc. here from thereafter. Please create an account at the new site to continue discussion.

micseydel

Posts: 3000
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

### Re: [Problem] Exercise in python

I used several methods: loops, using lists to insert each different pattern and number of occurrences ...

But I could not find the right way to respond to exercie.
Where can I start my code?

I speak French.
Kopp92i

Posts: 4
Joined: Fri Nov 01, 2013 11:50 pm

### Re: [Problem] Exercise in python

You should post the solution you've tried that you think is the closest to working. Post the traceback with it, in code tags too.
Due to the reasons discussed here we will be moving to python-forum.io on October 1, 2016.

This forum will be locked down and no one will be able to post/edit/create threads, etc. here from thereafter. Please create an account at the new site to continue discussion.

micseydel

Posts: 3000
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

### Re: [Problem] Exercise in python

How do you find the code so far? Is it optimal? Have I added a few things too which I could do without?
I want to have sub-lists that contains the number of occurrences and adjacent grounds corresponding to the number of occurrences in the list of "patterns" . Otherwise, my program does not run properly and I can't continue.

PS: the variable names are not always explicit, I will modify it afterwards.
Last edited by Kopp92i on Thu Nov 21, 2013 9:52 pm, edited 1 time in total.
Kopp92i

Posts: 4
Joined: Fri Nov 01, 2013 11:50 pm

### Re: [Problem] Exercise in python

I would replace
Code: Select all
`   n = len(a)   i=0   while i < n:      if count < N:         nombre.append(a[i])         if (a[i][0] == a[i+1][0]):            count += 1      i+=1`

with
Code: Select all
`   for pat, next_pat in zip(a, a[1:]):      if count < N:         nombre.append(pat)         if pat == next_pat:            count += 1`

(If memory is an issue, use islice from itertools, and if you're using Python 2 even though it looks like Python 3, izip from itertools as well.)

The first function can at least be simplified to
Code: Select all
`def differents_patterns(A,B,SEQ):   patterns = []   for m in range(A,B+1):      for x in range(2**m):         s = bin(x)[2:].zfill(m)         count = SEQ.count(s)         patterns.append([count, s])      patterns.sort(reverse=True)   return patterns`

I think that the best way though to solve this problem is with a trie.
Due to the reasons discussed here we will be moving to python-forum.io on October 1, 2016.

This forum will be locked down and no one will be able to post/edit/create threads, etc. here from thereafter. Please create an account at the new site to continue discussion.

micseydel

Posts: 3000
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA