Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| b74a428c2b | |||
|
|
65a4c79916 | ||
|
|
065447534c | ||
| 43cb9a9b56 | |||
| 430767c1e4 | |||
|
|
473022d019 |
43
src/codewars/pagination_helper.py
Normal file
43
src/codewars/pagination_helper.py
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
# Abgabe - PaginationHelper Aufgabe von Codewars
|
||||||
|
# Link dazu: https://www.codewars.com/kata/515bb423de843ea99400000a
|
||||||
|
# Leart Ramushi
|
||||||
|
|
||||||
|
# TODO: complete this class
|
||||||
|
import math
|
||||||
|
|
||||||
|
|
||||||
|
class PaginationHelper:
|
||||||
|
|
||||||
|
# The constructor takes in an array of items and an integer indicating
|
||||||
|
# how many items fit within a single page
|
||||||
|
def __init__(self, collection, items_per_page):
|
||||||
|
self.collection = collection
|
||||||
|
self.items_per_page = items_per_page
|
||||||
|
|
||||||
|
# returns the number of items within the entire collection
|
||||||
|
def item_count(self):
|
||||||
|
return len(self.collection)
|
||||||
|
|
||||||
|
# returns the number of pages
|
||||||
|
def page_count(self):
|
||||||
|
return math.ceil(len(self.collection) / self.items_per_page)
|
||||||
|
|
||||||
|
# returns the number of items on the given page. page_index is zero based
|
||||||
|
# this method should return -1 for page_index values that are out of range
|
||||||
|
def page_item_count(self, page_index):
|
||||||
|
if page_index < 0 or page_index >= self.page_count():
|
||||||
|
return -1
|
||||||
|
if page_index == self.page_count() - 1:
|
||||||
|
rest = len(self.collection) % self.items_per_page
|
||||||
|
if rest == 0:
|
||||||
|
return self.items_per_page
|
||||||
|
else:
|
||||||
|
return rest
|
||||||
|
return self.items_per_page
|
||||||
|
|
||||||
|
# determines what page an item at the given index is on. Zero based indexes.
|
||||||
|
# this method should return -1 for item_index values that are out of range
|
||||||
|
def page_index(self, item_index):
|
||||||
|
if item_index < 0 or item_index >= len(self.collection):
|
||||||
|
return -1
|
||||||
|
return item_index // self.items_per_page
|
||||||
51
src/codewars/vigenere_cipher_helper.py
Normal file
51
src/codewars/vigenere_cipher_helper.py
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
# Abgabe - Vigenère Cipher Helper
|
||||||
|
# Link dazu: https://www.codewars.com/kata/52d1bd3694d26f8d6e0000d3
|
||||||
|
# Leart Ramushi
|
||||||
|
|
||||||
|
|
||||||
|
class VigenereCipher(object):
|
||||||
|
def __init__(self, key, alphabet):
|
||||||
|
self.key = key
|
||||||
|
self.alphabet = alphabet
|
||||||
|
self.alp_len = len(alphabet)
|
||||||
|
pass
|
||||||
|
|
||||||
|
def encode(self, text):
|
||||||
|
result = ""
|
||||||
|
key_pos = 0
|
||||||
|
print(text)
|
||||||
|
|
||||||
|
for pos in range(len(text)):
|
||||||
|
currrent_char = text[pos]
|
||||||
|
if currrent_char in self.alphabet:
|
||||||
|
key_char = self.key[key_pos % len(self.key)]
|
||||||
|
# print(currrent_char, " - ", key_char)
|
||||||
|
text_index = self.alphabet.index(currrent_char)
|
||||||
|
key_index = self.alphabet.index(key_char)
|
||||||
|
# print(text_index, " - ", key_index)
|
||||||
|
update_index = (text_index + key_index) % len(self.alphabet)
|
||||||
|
result += self.alphabet[update_index]
|
||||||
|
else:
|
||||||
|
result += currrent_char
|
||||||
|
key_pos += 1
|
||||||
|
print(result)
|
||||||
|
return result
|
||||||
|
|
||||||
|
def decode(self, text):
|
||||||
|
result = ""
|
||||||
|
key_pos = 0
|
||||||
|
|
||||||
|
for pos in range(len(text)):
|
||||||
|
currrent_char = text[pos]
|
||||||
|
if currrent_char in self.alphabet:
|
||||||
|
key_char = self.key[key_pos % len(self.key)]
|
||||||
|
# print(currrent_char, " - ", key_char)
|
||||||
|
text_index = self.alphabet.index(currrent_char)
|
||||||
|
key_index = self.alphabet.index(key_char)
|
||||||
|
# print(text_index, " - ", key_index)
|
||||||
|
update_index = (text_index - key_index) % len(self.alphabet)
|
||||||
|
result += self.alphabet[update_index]
|
||||||
|
else:
|
||||||
|
result += currrent_char
|
||||||
|
key_pos += 1
|
||||||
|
return result
|
||||||
Loading…
x
Reference in New Issue
Block a user