Question
Need help with these Python methods. Thanks! edit: unfortunately this was all the info provided. the functions s11x1(n), s11x(n), and sx1(n) are really the functions
Need help with these Python methods. Thanks!
edit: unfortunately this was all the info provided. the functions s11x1(n), s11x(n), and sx1(n) are really the functions that are to be implemented to get the needed output. I believe the other functions in the code provided that areabove the ones I just listed can be used as helper methods. this program is dealing with bitstrings. example '0001', '0010', '0011', etc.
example: s11x(n): should output the set of all length n>=2 bitstrings that start with 11. so if n = 4, it needs to return the set of all bitstrings that start with '11' and are length 4. like ['1101', '1111']
n is defined in main with "n = int(sys.argv[1])" so we must pass a parameter in through our IDE or terminal for n. n must be >=2
import sys
''' provided strings are immutable, but lists are mutable replace(s,i,c) turns string s into a list, replaces s[i] with c, then turns the list back into a string and returns it ''' def replace(s,i,c): s=list(s) s[i]=c s = ''.join(s) return s
''' return a string with n c-s ''' def stringn(n,c):
''' given bit string bs of size n, return the next (boolean increment) e.g. '00000' -> '00001' '01011' -> '01100' '11111' -> '00000' ''' def next(bs,n):
''' return the set of all length n>=2 bitstrings that start with '11' or end with '1' ''' def s11x1(n):
''' return the set of all length n>=2 bitstrings that start with '11' ''' def s11x(n):
''' return the set of all length n>=2 bitstrings that end with '1' ''' def sx1(n):
''' provided ''' if __name__ == "__main__": # sets of bit strings size n n = int(sys.argv[1]) if n<2: print( 'argument must be>= 2' ) sys.exit() s3 = s11x1(n) print( 'len(s3) =', len(s3) ) s2 = s11x(n) print( 'len(s2) =', len(s2) ) s1 = sx1(n) print( 'len(s1) =', len(s1) ) print( 'len(s1&s2):', len(s1&s2) ) print( 's3 == s2|s1:', s3 == s2|s1 ) print( 'len(s3) == len(s2)+len(s1)-len(s2&s1):', len(s3) == len(s2)+len(s1)-len(s2&s1) )
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started