How to implementation linear search in String Class Python - python

I want to search for specific characters in a string list. For example, string_list = ['sasasd']; I want to search for 'sa'. The linear function will return True, but I tried many times, It can not return True.
class String:
def __init__(self, str_value = []):
self.value = str_value
def search_data(self,target_value):
m = len(self.value)
for i in range(m):
if self.value[i] == target_value:
return True
return False
value_data = ['dasdasd']
my_str = String(value_data)
result = my_str.search_data('da')
print(result)

You have a few problems in your code.
str_value is a list and you initialize the value of the class attribute like this: self.value = str_value. So, when you give it the value ['dasdasd'], you end up having a list with one string element.
When declaring the m variable, m = len(self.value), you compute the number of elements from that list NOT the number of characters from the string value inside the list (m = len(['dasdasd']) = 1).
Also, the for that searches for the target_value string inside the self.value string is incorrect.
Quick fix to correct the current code:
class String:
def __init__(self, str_value = []):
self.value = str_value
def search_data(self, target_value):
# assumption here that self.value has the format: ['string'] (list with 1 string element)
if self.value[0].find(target_value) == 0:
return True
return False
value_data = ['dasdasd']
my_str = String(value_data)
result = my_str.search_data('da')
print(result)
Tested and it outputs True.

Related

How do I write a class function that returns a value as soon as the class is called?

I have to complete the following challenge:
The object created from a class C contains an array of integers, passed in when the object is created. Define a text representation of an object that returns an expression that sums up all the numbers in an array, in the order as in the array.
Example:
C([5,12]) -> "5+12=17"
C([6,0,15]) -> "6+0+15=21"
I tried to return do this in the __init__() function but it cannot return a value.
class C():
def __init__(self, array):
self.array = array
# ctreating the text
sum = 0
text = ""
for i in range(len(array)):
sum += array[i]
text += str(array[i])
if i < len(array):
text += "+"
text += f"={sum}"
# trying to return the ready text
return(text)
C([5, 12])
It kind of works if I print() it intead of return but I'm supposed to return the value.
You are very close, you just need to separate initialization in __init__ and computations in another function:
class C():
def __init__(self, array):
self.array = array
def print_sum(self):
# creating the text
sum = 0
text = ""
for i in range(len(self.array)):
sum += array[i]
text += str(self.array[i])
if i < len(self.array):
text += "+"
text += f"={sum}"
# trying to return the ready text
return(text)
print(C([5, 12]).print_sum())
Another possibility as #John Gordon mentioned is:
class C():
def __init__(self, array):
self.array = array
def __str__(self):
# creating the text
sum = 0
text = ""
for i in range(len(self.array)):
sum += array[i]
text += str(self.array[i])
if i < len(self.array):
text += "+"
text += f"={sum}"
# trying to return the ready text
return(text)
print(C([5, 12]))
Now just printing the class will automatically print the string representation of the sum.

a program to check if a reversed string is a palindrome or not using Stack Machine in python

can u help me to solve the following
am trying to get a string input from user, then push the string into the StackMachine so as to check the validity of the string. if the String passes the rules defined, the output string from the StackMachine is to be checked if its a palindrome or not ..this is what i have tried so far
note you can only use the function in the StackMachine class and not another method to determine the validity of the string. The condition to accept a string is that you have read all the input string and the stack is empty
```
class StackMachine(object):
SMRules = {} # dictionary of SM rules
def __init__(self):
self.Stack = ['S'] # populate stack with initial 'S'
self.size = 1 # set size of stack to 1
self.SMRules = {} # Place rules here
def pop(self):
if len(self.Stack) <1:
pass
else:
self.Stack.pop(0)
self.size-= 1 # reduce stack size by 1
return
def peek(self):
ss = ""
if len(self.Stack) < 1:
return ""
else:
ss = self.Stack
return ss[0]
def stackIsEmpty(self):
if len(self.Stack) == 0:
return True
else:
return False
def push(self,str):
sStr = str[::-1] # slicing
for chr in sStr:
self.Stack.insert(0,chr) # push string onto top of stack
self.size = len(self.Stack)
return
def printStack(self):
print("Stack: [",end='')
for item in self.Stack:
print(item,end='')
print("]")
return
def printRules(self):
print("SM Rules:")
rn = 1
for key, value in self.SMRules.items():
print("Rule",rn,"%4s" % key,"|", value)
rn += 1
return
def main():
Stk = StackMachine()
text =str(input('Please enter the string: '))
for character in text:
Stk.push(character)
reversed_text = ''
while not Stk.stackIsEmpty():
reversed_text = reversed_text + Stk.pop()
if text == reversed_text:
print('The string is a palindrome.')
else:
print('The string is not a palindrome.')
if __name__ == '__main__':
main()
Am getting the following Error and i dont know how to solve it
```
File "C:\Users\user\Stack-Machine\StackMachine.py", line 91, in main
reversed_text = reversed_text + Stk.pop()
TypeError: can only concatenate str (not "NoneType") to str`enter code here`
my question is
1.how do I solve the error?
2.how to print string chracter by character from the stackMachine
3.successfully check if the processed string from the Stack Machine is a palindrome or not
As defined, StackMachine.pop() returns None.
When you do reversed_text + Stk.pop(), even though reversed_text is a string, Stk.pop() is None, which causes the TypeError you are seeing.
Why do you need to use the so called StackMachine class to check if a string is a palindrome? There are simpler ways to check if a string is a palindrome.
If all you want to do is get a string input and determine whether it's a palindrome, you can do so like this:
text = input()
isPalindrome = text == text[::-1]

NameError: name 'self' is not defined at the last block only?

Why it tells me "NameError: name 'self' is not defined" when I run this code?
I don't know why it's all fine except the last block (update method) gives me that error anyone can help?
This code is a game that takes in the lenth of the hand and gives you a random letters to create a word from the hand ,when you enter an currect word, the letters used in the word get removed automaticlly till the words
import random
class Hand(object):
self.hand = {}
def __init__(self, n):
'''
Initialize a Hand.
n: integer, the size of the hand.
'''
assert type(n) == int
self.HAND_SIZE = n
self.VOWELS = 'aeiou'
self.CONSONANTS = 'bcdfghjklmnpqrstvwxyz'
# Deal a new hand
self.dealNewHand()
def dealNewHand(self):
'''
Deals a new hand, and sets the hand attribute to the new hand.
'''
# Set self.hand to a new, empty dictionary
self.hand = {}
# Build the hand
numVowels = self.HAND_SIZE // 3
for i in range(numVowels):
x = self.VOWELS[random.randrange(0,len(self.VOWELS))]
self.hand[x] = self.hand.get(x, 0) + 1
for i in range(numVowels, self.HAND_SIZE):
x = self.CONSONANTS[random.randrange(0,len(self.CONSONANTS))]
self.hand[x] = self.hand.get(x, 0) + 1
def setDummyHand(self, handString):
'''
Allows you to set a dummy hand. Useful for testing your implementation.
handString: A string of letters you wish to be in the hand. Length of this
string must be equal to self.HAND_SIZE.
This method converts sets the hand attribute to a dictionary
containing the letters of handString.
'''
assert len(handString) == self.HAND_SIZE, "Length of handString ({0}) must equal length of HAND_SIZE ({1})".format(len(handString), self.HAND_SIZE)
self.hand = {}
for char in handString:
self.hand[char] = self.hand.get(char, 0) + 1
def calculateLen(self):
'''
Calculate the length of the hand.
'''
ans = 0
for k in self.hand:
ans += self.hand[k]
return ans
def __str__(self):
'''
Display a string representation of the hand.
'''
output = ''
hand_keys = sorted(self.hand.keys())
for letter in hand_keys:
for j in range(self.hand[letter]):
output += letter
return output
def update(self, word):
"""
Does not assume that self.hand has all the letters in word.
Updates the hand: if self.hand does have all the letters to make
the word, modifies self.hand by using up the letters in the given word.
Returns True if the word was able to be made with the letter in
the hand; False otherwise.
word: string
returns: Boolean (if the word was or was not made)
"""
for i in word :
if self.hand.get(i , 0) == 0 :
return False
else :
self.hand[i] -=1
return True
I think it's this line:
self.hand = {}
at the top of your class that is the problem. There is no self at the point when the class is being defined. This line should be inside your __init__ method.
def __init__(self, n):
'''
Initialize a Hand.
n: integer, the size of the hand.
'''
self.hand = {}
# ... and the rest

Encrypting a Text File in Python, Preferably with a Module [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
Context:
I have a class, in which I have to make a password protected something or other.
(My instructor is not very specific.) The reason for me asking for a method in which I could use a standard import statement was so I could just upload a single folder that only contained a text file and the program itself. I want to do something as follows:
#Import Statements Here
outFile = open('myFile.txt',"wt");
#Here is the tricky part.
#In place of all of these comments,
#I want to encrypt the file with a key given by a user(in this case,givenKey):
givenKey = input("Please input your key:>>>");
outFile.close();
Resolution:
The answer by Sasszem was the one that worked for me. Look into the comments
for a simplified explanation of the main answer. The person who gave me his/her custom made code (I cant remember who gave it to me, sorry.) had a good idea. But I don't like using code I don't understand. And finally, the person who gave me the Cryptography module as an idea wasn't wrong, but I use python on Windows.
A simple way to encrypt data is to add a constant to every byte.
You can generate some random byte from the pwd and then add it to every byte in the input. It won't be strong, but it's simple.
If you make something like your program generates random numbers to add to the bytes after seeding your pwd to the random generator, your instructor will be impressed.
To decode, simply subtract the same numbers from the bytes.
Package cryptography provides support for encryption/decryption: https://pypi.python.org/pypi/cryptography. I understand that it is included in Anaconda.
Here is a module I wrote a while back. It uses only built-in python modules. I give you permission to use it!
import string, random
class Round(object):
def __init__(self, *seqs):
self.position = 0
self.data = [i for x in seqs for i in x]
self.len = len(self.data)
def __repr__(self):
return str(self.data)
def __iter__(self):
self.position = 0
return self
def is_item(self, item):
if str(self.data[self.position]) == str(item):
return True
return False
def __getitem__(self, index):
if index < self.len-1 and index >= 0:
return self.data[index]
else:
while index < 0:
index += self.len-1
while index > self.len-1:
index -= self.len-1
return self.data[index]
def next(self):
if self.position >= self.len-1:
self.position = 0
raise StopIteration
else:
self.position += 1
return self.data[self.position-1]
class JCripter(object):
def __init__(self, string):
self.string = string
self.generate_key_set()
self.encrypted = False
def generate_key_set(self):
self.alphabet = list(string.ascii_lowercase)
self.numbers = [str(x) for x in range(10)]
self.special_characters = ['"',"'",',','?','.',
' ','(',')',':',';',
'!','#','#','$','%',
'^','&','*','_','-',
'+','=','<','>','~',
'`','{','[','}',']',
'\\','|']
self.key_base = Round(self.alphabet, self.numbers, self.special_characters)
def get_key_index(self, key):
for i in self.key_base:
if isinstance(key, int):
if i == self.key_base[key]:
return self.key_base.position-1
elif i == key.lower():
return self.key_base.position-1
else:
print 'not found'
def __repr__(self):
return self.string
def _encrypt(self, string, func, *args):
if string == None:
string = self.string
if string == None:
return
string = string.lower()
n_string = func(string, *args)
self.encrypted = not self.encrypted
self.string = n_string
return n_string
class CeaserCypher(JCripter):
def __init__(self, string, shift=None):
JCripter.__init__(self, string)
if shift == None:
self.shift = random.randint(0, self.key_base.len)
else:
self.shift = shift
def encrypt(self, string=None):
def inner(string):
n_string=''
for i in string:
if self.encrypted == True:
n_string += self.key_base[self.get_key_index(i)-self.shift]
else:
n_string += self.key_base[self.get_key_index(i)+self.shift]
return n_string
return self._encrypt(string, inner)
class PseudoRandomCypher(JCripter):
def __init__(self, string, shifts=None):
if shifts == None:
self.shift = [random.randint(0, 500) for x in string]
else:
self.shift = shifts
JCripter.__init__(self, string)
def encrypt(self, string=None):
def inner(string):
ind = 0
n_string = ''
for i in string:
if ind >= len(self.shift)-1:
ind = 0
if self.encrypted == True:
n_string += self.key_base[self.get_key_index(i)-self.shift[ind]]
else:
n_string += self.key_base[self.get_key_index(i)+self.shift[ind]]
ind += 1
return n_string
return self._encrypt(string, inner)
class PolyAlphabeticCypher(JCripter):
def __init__(self, string, key, enc=False):
JCripter.__init__(self, string)
self.key=list(key)
self.encrypted = enc
def encrypt(self, string=None):
def inner(string):
index = 0
n_string = ''
for i in string:
if index >= len(self.key)-1:
index = 0
if self.encrypted == True:
n_string += self.key_base[self.get_key_index(i)-self.get_key_index(self.key[index])]
else:
n_string += self.key_base[self.get_key_index(i)+self.get_key_index(self.key[index])]
index += 1
return n_string
return self._encrypt(string, inner)
n = 'Hello world my name is anonymous!'
p = PolyAlphabeticCypher(n, 'super_secret_password')
print p.encrypt() #--> returns encrypted data
print p.encrypt() #--> decrypts data
#if you are decrypting a previously encrypted text
n = 'Some sensitive data'
first = PolyAlphabeticCypher(n, 'some pass')
my_data = first.encrypt()
second = PolyAlphabeticCypher(my_data, 'some pass', True)
print second.encrypt()

Is there something wrong with this while-loop?

When I execute this code, it prints 'Constructed', meaning it executed Trie Construction - then my terminal outputs nothing, it doesn't return or print any error, it's just blank, as if it's still working on the problem. Is there something wrong with the while loop? Is it that the 'trie' is an external variable?
trie is a list of nodes, a class I defined.
class node:
def __init__(self, parent, daughters, edge):
self.parent = parent
self.daughters = daughters
self.edge = edge
trie.append(self)
self.index = len(trie) - 1
patterns is a list of fixed strings.
def TrieConstruction(patterns, trie):
trie.append(node(0, [], 0))
for pattern in patterns:
currentNode = trie[0]
for base in pattern:
for daughter in currentNode.daughters:
if base == daughter.edge:
currentNode = daughter
break
else:
trie.append(node(currentNode, [], base))
currentNode = trie[-1]
print('Constructed.')
return
def PrefixTrieMatching(text, trie):
v = trie[0]
for index, base in enumerate(text):
if v.daughters == []:
pattern_out = []
climb(v.index)
return ''.join(pattern_out)
else:
for daughter in v.daughters:
if base == daughter.edge:
v = daughter
break
else:
print('No matches found.')
return
def climb(index):
if index == 0:
return
else:
pattern_out.append(node.edge)
climb(trie[index].parent)
def TrieMatching(text, trie):
while text != []:
PrefixTrieMatching(text, trie)
text = text[0:len(text) - 2]
print('Complete.')
return
print('Next, we generate a trie with the patterns, and then run the text over the trie to search for matches.')
trie = []
TrieConstruction(patterns, trie)
TrieMatching(text, trie)
EDIT:
Disregard my previous answer, if you are entering a string as text, it should be:
while text != "":
PrefixTrieMatching(text, trie)
text = text[0:len(text) - 2]
as the string would never be an empty list
You are doing more work than needed, just use while text which will return False only for an empty string and just slice your string slicing two chars from the end at a time:
def TrieMatching(text, trie):
while text:
PrefixTrieMatching(text, trie)
text = text[:-2]
An empty list, str, dict etc will always evaluate to False so you don't ever need to explicitly check if my_list != [], if my_str != "", if my_list and if my_str etc.. is sufficient.

Categories

Resources