[Problem] Exercise in python

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

[Problem] Exercise in python

Postby Kopp92i » Sat Nov 02, 2013 12:28 am

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: 01010010010001000111101100001010011001111000010010011110010000000
23: 00
15: 10 01
12: 100
11: 001 000 11
10: 010
8: 0100
7: 1001 0010
6: 0000 111
5: 1000 110 011
4: 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

Postby micseydel » Sat Nov 02, 2013 12:30 am

What attempt have you made at solving your problem?

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.
Join the #python-forum IRC channel on irc.freenode.net!
User avatar
micseydel
 
Posts: 1115
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: [Problem] Exercise in python

Postby Kopp92i » Sat Nov 02, 2013 4:25 pm

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

Postby micseydel » Sat Nov 02, 2013 4:30 pm

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.
Join the #python-forum IRC channel on irc.freenode.net!
User avatar
micseydel
 
Posts: 1115
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: [Problem] Exercise in python

Postby Kopp92i » Sun Nov 03, 2013 1:21 am

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

Postby micseydel » Sun Nov 03, 2013 10:20 pm

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.
Join the #python-forum IRC channel on irc.freenode.net!
User avatar
micseydel
 
Posts: 1115
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: [Problem] Exercise in python

Postby Kopp92i » Mon Nov 04, 2013 9:13 am

Thank you for your help!
Kopp92i
 
Posts: 4
Joined: Fri Nov 01, 2013 11:50 pm


Return to General Coding Help

Who is online

Users browsing this forum: Ofir and 2 guests