How to rotate a list(not 2D) 90 degree clockwise? - python

As a beginner in Python, I think the biggest problem I have is overcomplicating a problem when it can be done a lot simpler. I have not found a solution for a list that is not two-dimensional, hence why I chose to ask.
Here is an example of what I am trying to do:
# Before
alphabet = ["ABCDEFG",
"HIJKLMN",
"OPQRSTU"]
# After
rotated_alphabet = ["OHA",
"PIB",
"QJC",
"RKD",
"SLE",
"TMF",
"UNG"]
What I have done so far:
length_of_column = len(alphabet)
length_of_row = len(alphabet[0])
temp_list = []
x = -1
for i in range(length_of_column):
while x < length_of_row-1:
x += 1
for row in alphabet:
temp_list.append(row[x])
temp_list = temp_list[::-1]
Output
print(temp_list)
>>> ['U', 'N', 'G', 'T', 'M', 'F', 'S','L','E','R','K','D','Q','J','C','P','I','B', 'O', 'H', 'A']
I need to make the list above in the desired format.
-How would I do this?
-Is there a simpler way to do it?

You can just zip the list of strings, and it will make tuples character by character, then you'll only have to join the tuples in reverse order. Here it is in just one line:
rotated_alphabet = [''.join(list(i)[::-1]) for i in zip(*alphabet)]

A variant of #MuhammadAhmad answer will be to use reversed, as reversed works with iterables, no need to convert to a list.
alphabet = ["ABCDEFG",
"HIJKLMN",
"OPQRSTU"]
rotated = [''.join(reversed(a)) for a in zip(*alphabet)]
print(rotated)
Output
['OHA', 'PIB', 'QJC', 'RKD', 'SLE', 'TMF', 'UNG']

Related

Nested list loop indexing

I am trying to create a list of characters based on a list of words
i.e.
["BOARD", "GAME"] -> [["B","O"...], ["G","A","M"...]
From my understanding, I have an IndexError because my initial boardlist does not contain a predetermined the amount of lists.
Is there a way for to create a new list in boardlist according to number of objects in board?
I don't know if I'm being clear.
Thank you.
board=["BOARD", "GAME"]
boardlist=[[]]
i=0
for word in board:
for char in word:
boardlist[i].append(char)
i=i+1
print(boardlist)
IndexError: list index out of range
Note that this can be done in a much simpler way by taking a list of each string in the board, as the list constructor will be converting the input iterable, in this case a string, to a list of substrings from it:
l = ["BOARD", "GAME"]
[list(i) for i in l]
# [['B', 'O', 'A', 'R', 'D'], ['G', 'A', 'M', 'E']]
Let's also find a fix to your current approach. Firstly boardlist=[[]] is not a valid way of initializing a list (check what it returns). You might want to check this post. Also instead of incrementing a counter you have enumerate for that:
boardlist = [[] for _ in range(len(board))]
for i, word in enumerate(board):
for char in word:
boardlist[i].extend(char)
print(boardlist)
# [['B', 'O', 'A', 'R', 'D'], ['G', 'A', 'M', 'E']]

Calculate the difference between 2 strings (Levenshtein distance)

I am trying to calculate the distance between two strings. The distance/difference between two strings refers to the minimum number of character insertions, deletions, and substitutions required to change one string to the other.
The method I have tried is to: convert two strings into lists, compare lists, check the differences, then add the differences
first_string = "kitten"
second_string = "sitting"
list_1 = list(first_string)
list_2 = list(second_string)
print("list_1 = ", list_1)
print("list_2 = ", list_2)
print(" ")
lengths = len(list_2) - len(list_1)
new_list = set(list_1) - set(list_2)
print(lengths)
print(new_list)
difference = lengths + int(new_list)
print(difference)
the output I get is:
list_1 = ['k', 'i', 't', 't', 'e', 'n']
list_2 = ['s', 'i', 't', 't', 'i', 'n', 'g']
1
{'e', 'k'}
Of which then I am trying to find out how to add these differences so it equals 3. I don't know how to make the outputs similar to add them together (adding 1 with {'e', 'k'} to equal a distance of 3).
You're almost there. Calculate the length of new_list using len() like you did with lengths:
difference = lengths + len(new_list)
Looks like you just need to change this line:
difference = lengths + int(len(new_list))
That should give you 3 like you want :)
This is referred to as the Levenshtein distance. Check out this implementation as further reading.

Combinations of a String

The following was a job interview question which I struggled with.
(Unnecessary switching between list and set, tested it and realised it was missing an expected output, too many steps).
If possible, looking for the proper answer or maybe a guide on how I should have tackled the problem. Thank you.
Question: Give a String, find all possible combinations from it (Front and Reverse).
Print all combinations and total count of combinations. Order doesn't matter.
Example s = 'polo'
Front Answer = 'p', 'po', 'pol', 'polo', 'ol',
'olo', 'lo', 'o', 'l'.
Reverse Answer: 'o', 'ol', 'olo', 'olop',
'lop', 'op', 'p', 'l'.
My answer:
count = 0
count2 = -1
length = len(s)
my_list = []
for i in s:
temp = s[count:]
temp2 = s[:count2]
my_list.append(i)
my_list.append(temp)
my_list.append(temp2)
count += 1
count2 -= 1
my_set = set(my_list)
for f in my_set:
print(f)
print(len(my_set)) # Answer for front
new_list = []
for f in my_set:
new_list.append(f[::-1])
print('Reverse Result:')
for f in new_list:
print(f)
print(len(new_list)) # Answer for reverse
You can do this with two nested for-loops. One will loop through the start indexes and the nested one loops through the end indexes (starting from the start + 1 going to the length of s +1 to reach the very end).
With these two indexes (start and end), we can use string slicing to append that combination to the list forward. This gives you all the combinations as you see below.
To get the reversed ones, you could do a for-loop as you have done reversing the order of the forward ones, but to save the space, in the code below, we just append the same index but sliced from the reversed s (olop).
s = "polo"
forward = []
backward = []
for start in range(len(s)):
for end in range(start+1, len(s)+1):
forward.append(s[start:end])
backward.append(s[::-1][start:end])
print(forward)
print(backward)
print(len(forward) + len(backward))
which outputs:
['p', 'po', 'pol', 'polo', 'o', 'ol', 'olo', 'l', 'lo', 'o']
['o', 'ol', 'olo', 'olop', 'l', 'lo', 'lop', 'o', 'op', 'p']
20
If you really wanted to make the code clean and short, you could do the same thing in a list-comprehension. The logic remains the same, but we just compress it down to 1 line:
s = "polo"
forward = [s[start:end] for start in range(len(s)) for end in range(start+1, len(s)+1)]
backward = [c[::-1] for c in forward]
print(forward)
print(backward)
print(len(forward) + len(backward))
which gives the same output as before.
Hope this helps!
Try this:
string = "polo"
x = [string[i:j] for i in range(len(string)) for j in range(len(string),0,-1) if j > i ]
x.sort()
print("Forward:",x[::-1])
print("Reverse:",x[::])

python function that return all initial segments of a list

I am trying to produce a function that take as input a list of string and returns all the initial segments of that list.
i.e the output should be:
([[], ['k'], ['k', 'i'], ['k', 'i', 'm'], ['k', 'i', 'm', 'i']])
I have done the following but it is not correct because I get number instead of characters.
def funv(k):
return [[i for i in range(i)] for i in range(len(k))]
Can anyone tell me what can I do to correct it?
This should work:
[list(k[:i]) for i in range(1, len(k)+1)]
I really have no idea what you are doing, but it sounds like you are just missing one small thing. Instead of returning i in your inner list comprehension, you want to return k[i]. i is the position where k[i] would be the character at position i.
def funv(k):
return [[k[i] for i in range(i)] for i in range(len(k))]

Compare lists to find common elements in python [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Python - Intersection of two lists
i'm trying to compare two lists in order to find the number of elements they have in common.
The main problem I'm having is when either list contains repeated elements, for example
A = [1,1,1,1] and
B = [1,1,2,3]
using the code
n = 0
for x in A:
if x in B:
n += 1
print n
gives me the output that n = 4, as technically all elements of A are in B
I'd like to get the output that n = 2, preferably without using sets, Is there anyway I can adapt my code, or a new way of thinking about the problem to achieve this?
Thanks
It's not entirely clear what your specification is, but if you want the number of elements in A that appear in B, without regard to order, but with regard to multiplicity, use collections.Counter:
>>> from collections import Counter
>>> A = [1,1,1,1]
>>> B = [1,1,2,3]
>>> C = Counter(A) & Counter(B)
>>> sum(C.itervalues())
2
>>> list(C.elements())
[1, 1]
Here is an efficient (O(n logn)) way to do it without using sets:
def count_common(a, b):
ret = 0
a = sorted(a)
b = sorted(b)
i = j = 0
while i < len(a) and j < len(b):
c = cmp(a[i], b[j])
if c == 0:
ret += 1
if c <= 0:
i += 1
if c >= 0:
j += 1
return ret
print count_common([1,1,1,1], [1,1,2,3])
If your lists are always sorted, as they are in your example, you can drop the two sorted() calls. This would give an O(n) algorithm.
Here's an entirely different way of thinking about the problem.
Imagine I've got two words, "hello" and "world". To find the common elements, I could iterate through "hello", giving me ['h', 'e', 'l', 'l', 'o']. For each element in the list, I'm going to remove it from the second list(word).
Is 'h' in ['w', 'o', 'r', 'l', 'd']? No.
Is 'e' in ['w', 'o', 'r', 'l', 'd']? No.
Is 'l' in ['w', 'o', 'r', 'l', 'd']? Yes!
Remove it from "world", giving me ['w', 'o', 'r', 'd'].
is 'l' in ['w', 'o', 'r', 'd']? No.
Is 'o' in ['w', 'o', 'r', 'd']?
Yes! Remove it ['w', 'o', 'r', 'd'], giving me ['w', 'r', 'd']
Compare the length of the original object (make sure you've kept a copy around) to the newly generated object and you will see a difference of 2, indicating 2 common letters.
So you want the program to check whether only elements at the same indices in the two lists are equal? That would be pretty simple: Just iterate over the length of the two arrays (which I presume, are supposed to be of the same length), say using a variable i, and compare each by the A.index(i) and B.index(i) functions.
If you'd like, I could post the code.
If this is not what you want to do, please do make your problem clearer.

Categories

Resources