Skip to content

Instantly share code, notes, and snippets.

@seven0525
Last active May 24, 2018 03:00
Show Gist options
  • Save seven0525/78303bf6e064def58546869e4c1c6fb9 to your computer and use it in GitHub Desktop.
Save seven0525/78303bf6e064def58546869e4c1c6fb9 to your computer and use it in GitHub Desktop.
「自作Python100本ノック」12日目(81本〜88本目) ref: https://qiita.com/ahpjop/items/e86f361e903e3adb6f84
from itertools import combinations
def twosums(x, target):
for item in combinations(x, 2):
if sum(item) == target:
return item
nums = [2, 7, 11, 15]
target = 9
twosums(nums, target)
def reverse_integer(x):
return int(str(x)[::-1])
reverse_integer(1534236469)
def roman_to_int(roman):
values={'M': 1000, 'D': 500, 'C': 100, 'L': 50,
'X': 10, 'V': 5, 'I': 1}
"""Convert from Roman numerals to an integer."""
numbers = []
for char in roman:
numbers.append(values[char])
total = 0
for num1, num2 in zip(numbers, numbers[1:]):
if num1 >= num2:
total += num1
else:
total -= num1
return total + num2
roman_to_int("XI")
def isvalid(x):
table = {'(': ')', '{': '}', '[': ']'}
stack = []
for elm in x:
if elm in table.keys():
stack.append(table[elm])
elif elm in table.values() and elm != stack.pop():
return False
return False if len(stack) else True
isvalid('[aaa]'), isvalid('('), isvalid('()[]{}'), isvalid('(]'), isvalid('([)]'), isvalid('{()}')
stre = 'パタトクカシーー'
print(stre[0::2])#str[::2]でも同様
str1 = 'パトカー'
str2 = 'タクシー'
print(''.join([a + b for a, b in zip(str1, str2)]))
strr = "Now I need a drink, alcoholic of course, after the heavy lectures involving quantum mechanics."
strr = strr.replace('.', "")
strr = strr.replace(',', "")
strr = strr.split()
a_list = []
for word in strr:
a_list.append(len(word))
print(list)
import random
text = "I couldn't believe that I could actually understand what I was reading : the phenomenal power of the human mind ."
words = text.split()
shuffled_list = []
for word in words:
if len(word) < 4:
pass
else:
char_list = list(word)
mid_list = char_list[1:-1]
random.shuffle(mid_list)
word = word[0] + "".join(mid_list) + word[-1]
shuffled_list.append(word)
shuffled_str = " ".join(shuffled_list)
print(shuffled_str)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment