Checking the same elements in a list : python - python

Hey there i'm so new in coding and i want make program comparing two lists elements and returning the same elements in them.
so far i writed this code but i'm having problem with algoritm because it is set operation and i can't find actual same elements with intersection function.
in my code i want to look for each string and finding similarity of them.
what i've tried to do is :
input="AGA"
input1="ACA"
input=input_a
if len(input1) == len(input):
i = 0
while i < len(input1):
j = 0
while j < len(input_a):
input_length = list(input_a)
if input1[i] != input_a[j]:
if input1[i] in input_a:
print "1st %s" % input_length
print "2nd %s" % set(input1)
intersection = set(DNA_input_length).intersection(set(input1))
print intersection
total = len(intersection)
print (float(total) / float(
len(input1))) * 100, "is the similarity percentage"
break
DNA_input_length.remove(input_a[i])
j = j + 1
break
what is wrong with my code is actually the intersection part i guess and
i want to see as common elements which are included each list for input and input1 = A,A (2 A's both) however, i get just one A..
How can i improve this code to evaluating common elements which is Two A not one. I really need your help..

I would define similarity as the the hamming distance between the words (which I think is what you want
word1 = "AGA"
word2 = "ACAT"
score = sum(a==b for a,b in zip(word1,word2)) + abs(len(word1)-len(word2))

If you just need to find the intersecting elements of 2 flat lists, do:
a = "AGA"
b = "ACA"
c = set(a) & set(b)
print(c)
> {'A'}

Related

Compare each element of a list in existing order with elements of a second list in order as long as items in lists are equal

Compare each element of a list in existing order with elements of a second list in existing order as long as items in lists are equal. Stop if they're not equal and give me as result the index and name of the last match.
I thought it's straightforward with a while loop but it seems like this has to be approached with a for-loop.
My List one I want to compare:
nk_script_file_path
['P:', 'Projects', '2019_projects', '1910_My_Project', '01_Production_IN', '01_OFX', '01_Comp', '00_Nuke', 'relink_test_v001.nk']
My second list I want to compare it to:
node_filepath
['P:', 'Projects', '2019_projects', '1910_My_Project', '02_Production_OUT', '01_OFX', '01_Comp', '00_Nuke', '040_ALY', '040_ALY_040_HROTERRORBLADE', '040_ALY_040_HROTERRORBLADE_prev_Gamma22_apcs_mov', '040_ALY_040_HROTERRORBLADE_prev_v14_Gamma22_apcs.mov']
What I've tried
nk_script_file_path = r"P:/Projects/2019_projects/1910_My_Project/01_Production_IN/01_OFX/01_Comp/00_SO/relink_test_v001.nk".split("/")
node_filepath = r"P:/Projects/2019_projects/1910_My_Project/02_Production_OUT/01_OFX/01_Comp/00_S=/040_ALY/040_ALY_040_HROTERRORBLADE/040_ALY_040_HROTERRORBLADE_prev_Gamma22_apcs_mov/040_ALY_040_HROTERRORBLADE_prev_v14_Gamma22_apcs.mov".split("/")
# Compare file paths
path_object = 0
while nk_script_file_path in node_filepath:
path_object += 1
print path_object
print node_filepath[path_object]
Result I'm looking for:
"3"
or
"1910_My_Project"
You can use zip() with enumerate() to find first index where's difference. In this example if no difference is found, value of i is equal to -1:
lst1 = ['P:', 'Projects', '2019_projects', '1910_My_Project', '01_Production_IN', '01_OFX', '01_Comp', '00_Nuke', 'relink_test_v001.nk']
lst2 = ['P:', 'Projects', '2019_projects', '1910_My_Project', '02_Production_OUT', '01_OFX', '01_Comp', '00_Nuke', '040_ALY', '040_ALY_040_HROTERRORBLADE', '040_ALY_040_HROTERRORBLADE_prev_Gamma22_apcs_mov', '040_ALY_040_HROTERRORBLADE_prev_v14_Gamma22_apcs.mov']
for i, (a, b) in enumerate(zip(lst1, lst2)):
if a != b:
break
else:
i = -1
print('First difference is at index:', i)
Prints:
First difference is at index: 4
nk_script_file_path= r"P:/Projects/2019_projects/1910_My_Project/01_Production_IN/01_OFX/01_Comp/00_SO/relink_test_v001.nk".split("/")
node_filepath = r"P:/Projects/2019_projects/1910_My_Project/02_Production_OUT/01_OFX/01_Comp/00_S=/040_ALY/040_ALY_040_HROTERRORBLADE/040_ALY_040_HROTERRORBLADE_prev_Gamma22_apcs_mov/040_ALY_040_HROTERRORBLADE_prev_v14_Gamma22_apcs.mov".split("/")
j = 0
for i in nk_script_file_path:
if i != node_filepath[j] :
j = j-1
break
else:
j += 1
print(nk_script_file_path[j])
print(j)

Algorithm problem for Deletion Distance between 2 string

I was solving this problem at Pramp and I have trouble figuring out the algorithm for this problem. I'll paste the problem description and how I kind of solved it. It's the correct solution. It is similar to the edit distance algorithm and I used the same approach. I just wanted to see what are other ways to solve this problem.
The deletion distance of two strings is the minimum number of characters you need to delete in the two strings in order to get the same string. For instance, the deletion distance between "heat" and "hit" is 3:
By deleting 'e' and 'a' in "heat", and 'i' in "hit", we get the string "ht" in both cases.
We cannot get the same string from both strings by deleting 2 letters or fewer.
Given the strings str1 and str2, write an efficient function deletionDistance that returns the deletion distance between them. Explain how your function works, and analyze its time and space complexities.
Examples:
input: str1 = "dog", str2 = "frog"
output: 3
input: str1 = "some", str2 = "some"
output: 0
input: str1 = "some", str2 = "thing"
output: 9
input: str1 = "", str2 = ""
output: 0
What I want to do in this solution, is to use dynamic programming in order to build a function that calculates opt(str1Len, str2Len). Notice the following:
I use dynamic programming methods to calculate opt(str1Len, str2Len), i.e. the deletion distance for the two strings, by calculating opt(i,j) for all 0 ≤ i ≤ str1Len, 0 ≤ j ≤ str2Len, and saving previous values
def deletion_distance(s1, s2):
m = [[0 for j in range(len(s2) +1)] for i in range(len(s1)+1)]
for i in range(len(s1)+1):
for j in range(len(s2)+1):
if i == 0:
m[i][j] = j
elif j == 0:
m[i][j] = i
elif s1[i-1] == s2[j-1]:
m[i][j] = m[i-1][j-1]
else:
m[i][j] = 1 + min(m[i-1][j], m[i][j-1])
return m[len(s1)][len(s2)]

Optimize python code to avoid runtime error

Given a string that might have multiple occurrences of the same character, return the closest same character of any indicated character in the string.
Given the string s and n number of queries. In each query, you are given an index a (where 0 <= a <= |s| ) of a character, and you need to print the index of the closet same character. If there are multiple answers, print the smallest one. Otherwise, print -1.
For example, string s = 'youyouy', with a given query 3: there are two matching character at indices 0 and 6, each 3 away, we choose the smallest one which is 0.
Here is my plan:
I put the string in a dictionary, the key is distinct letters in a string, values are letters corresponding indexes. When given a query, find the corresponding letter in the dictionary and return the closest value to the query.
def closest(s, queries):
res = []
dict2={}
#dict2 - letter - indexs
for i in range(len(s)):
if s[i] not in dict2:
dict2[s[i]]=[i]
else:
dict2[s[i]].append(i)
for num in queries:
#closet- denotes closet letter index
closet = math.inf
#num is out of range , append -1
if num > (len(s)-1):
res.append(-1)
continue
#this is the only one letter, append -1
letter=s[num]
if len(dict2[letter])==1:
res.append(-1)
continue
#temp = list for that letters
temp=dict2[s[num]]
index=temp.index(num) . #in the list, letter index's index in list
if index==0:
closet=temp[1]
elif index==(len(temp)-1):
closet=temp[index-1]
else:
distance1=num-temp[index-1] . #left
distance2=temp[index+1]-num . #right
if distance1 <= distance2:
closet=temp[index-1]
else:
closet=temp[index+1]
if closet == math.inf:
res.append(-1)
else:
res.append(closet)
return res
I got two runtime error. I am wondering if you could help me out to maybe reduce some run time ?
Also, I am looking for another suggestions! I have used Python for a while, and I am looking for a job (university new grad). Is java usually running faster than Python? Should I switch to Java?
Im trying to do as simple as i can , but i look like a bit complex. Though you question is avoiding runtime error , i want to present my idea
s='oooyyouoy'
k='0123456789'
def cloest(string,pos):
c = string[pos]
p1 , p2 = s[:pos] , s[pos+1:]
# reserve left part and find the closet one , add 1 because len(p1)=final_position + 1
l = len(p1) - (p1[::-1].find(c) + 1)
# find without reserve and add 1 because s[pos+1:]
r = (p2.find(c) + 1) + pos
# judge which one is closer if same chose left one
result = l if (pos - l) <= (r - pos) else r
if result == pos:
return -1
else:
return result
print(cloest(s,4))

How to do sum in Loop

I have a exercise ,Input data will contain the total count of pairs to process in the first line.
The following lines will contain pairs themselves - one pair at each line.
Answer should contain the results separated by spaces.
My code:
n = int(raw_input())
sum = 0
for i in range(n):
y = raw_input().split(" ")
for i in y:
sum = sum + int(i)
print sum
With my code , I come the Sum together, but I will that the results to come separated by spaces . Thanks for yours help .
with your current code what you get is the total sum of all the given numbers, to get the sum per line you need to initialize your counter in the outer loop, and then print it, and as you want to print all it in the same line there are several ways to do it, like save it in a list or telling print that don't print a new, line which is done by adding a , at the end like print x, with that in mind then the changes needed are
n = int(raw_input())
for i in range(n):
pairs = raw_input().split() #by default split use spaces
pair_sum = 0
for p in pairs:
pair_sum += int(p) # a += b is the same as a = a + b
print pair_sum,
print "" # to print a new line so any future print is not done in the same line as the previous one
that was the version with print per line, next is the version using list
n = int(raw_input())
resul_per_line = []
for i in range(n):
pairs = raw_input().split() #by default split use spaces
pair_sum = 0
for p in pairs:
pair_sum += int(p) # a += b is the same as a = a + b
resul_per_line.append( str(pair_sum) ) #conver each number to a string to use with join bellow
print " ".join(resul_per_line)
with either of the above let said for example that the input data is
3
1 2
40 50
600 700
then the result would be
3 90 1300
some parts of the above code can be simplify by using built in functions like map and sum, for example this part
pair_sum = 0
for p in pairs:
pair_sum += int(p)
can become
pair_sum = sum( map(int,pairs) )
Uh oh, it looks like you're reusing the same variable i in the inner loop as the outer loop -- this is bad practice and can lead to bugs down the road.
What you're doing currently is adding both elements in each pair to sum and then printing that at the end, you can fix this in two different ways.
You can sum each pair, convert the sum to a string, and then concatenate that with your the rest of the sums as strings, or
You can print the sum of each pair immediately after summing them with print sum, which will print the number without the newline so that you can print all the results on a single line.

Sorting numbers in python

I'm trying to get a function where if you do sort(listname) it would sort all the numbers inside that list from least to greatest.
I'm not sure whats wrong with mine, but I need some help as the output isn't actually least to greatest it does least to greatest for the first two numbers of the number.
Example :
If list has 23, 212, 44 in it than I sort it the output will be like this.
Output :
212,23,44
It should be 23, 44, 212.
Code:
def sort(my_list):
size = len(my_list)
for i in range(size):
for j in range(size-i-1):
if(my_list[j] > my_list[j+1]):
tmp = my_list[j]
my_list[j] = my_list[j+1]
my_list[j+1] = tmp
More code :
numbers=([])
amount=input("How many numbers are in your list? ")
print("")
counter = 0
ran = 0
while counter < int(amount):
counter = counter + 1
ran = ran + 1
num3 = input(str(ran) + ". Input: ")
try:
val = int(num3)
except ValueError:
num3 = input(str(ran) + ". Input: ")
sort(numbers)
numbers.append(num3)
That looks like your list doesn't contain any numbers but strings. Python doesn't try to guess what might be inside of those strings, so you get an odd sort order.
You have two options:
Convert list elements to int before comparing them (if(int(my_list[j]) > int(my_list[j+1])):)
Search this site for python natural sort to get answers how to implement "natural sorting" in Python.
If you just want it sorted, use sorted:
sorted_my_list = sorted(my_list,key=int)
Or to sort it, in place, use
my_list.sort(key=int)
If you want to fix your program, turn my_list into ints or at least compare integers
if ( int(my_list[j]) > int(my_list[j+1])):
Also, you can swap two variables in one statement:
my_list[j],my_list[j+1] = my_list[j+1],my_list[j]
I just used your code (Just added a return statement). And it returns the expected values:
def sort(my_list):
size = len(my_list)
for i in range(size):
for j in range(size-i-1):
if(my_list[j] > my_list[j+1]):
tmp = my_list[j]
my_list[j] = my_list[j+1]
my_list[j+1] = tmp
return my_list # Note I added this return statement
print sort([23,212,44])
>>>
[23, 44, 212]

Categories