itertools.product()
・問題
This tool computes the cartesian product of input iterables.
It is equivalent to nested for-loops.
For example, product(A, B)
returns the same as ((x,y) for x in A for y in B)
.
Sample Code
>>> from itertools import product
>>>
>>> print list(product([1,2,3],repeat = 2))
[(1, 1), (1, 2), (1, 3), (2, 1), (2, 2), (2, 3), (3, 1), (3, 2), (3, 3)]
>>>
>>> print list(product([1,2,3],[3,4]))
[(1, 3), (1, 4), (2, 3), (2, 4), (3, 3), (3, 4)]
>>>
>>> A = [[1,2,3],[3,4,5]]
>>> print list(product(*A))
[(1, 3), (1, 4), (1, 5), (2, 3), (2, 4), (2, 5), (3, 3), (3, 4), (3, 5)]
>>>
>>> B = [[1,2,3],[3,4,5],[7,8]]
>>> print list(product(*B))
[(1, 3, 7), (1, 3, 8), (1, 4, 7), (1, 4, 8), (1, 5, 7), (1, 5, 8), (2, 3, 7), (2, 3, 8), (2, 4, 7), (2, 4, 8), (2, 5, 7), (2, 5, 8), (3, 3, 7), (3, 3, 8), (3, 4, 7), (3, 4, 8), (3, 5, 7), (3, 5, 8)]
Task
You are given a two lists and . Your task is to compute their cartesian product X.
Example
A = [1, 2]
B = [3, 4]
AxB = [(1, 3), (1, 4), (2, 3), (2, 4)]
Note: and are sorted lists, and the cartesian product's tuples should be output in sorted order.
Input Format
The first line contains the space separated elements of list .
The second line contains the space separated elements of list .
Both lists have no duplicate integer elements.
Constraints
Output Format
Output the space separated tuples of the cartesian product.
Sample Input
1 2
3 4
Sample Output
(1, 3) (1, 4) (2, 3) (2, 4)
・コメント
2行の入力に対してそのコンビネーションを返すプログラム。
問題文のsample codeが分かりやすいので、スラスラ書ける問題。
唯一詰まるポイントは、sample codeと同様にして得られたリストからタプルをjoinで抜き出すことができないので、文字列に変換してから抜き出すことに注意。
・URL
https://www.hackerrank.com/challenges/itertools-product/problem
String Formatting
・問題
Input Format
A single integer denoting .
Constraints
Output Format
Sample Input
17
Sample Output
1 1 1 1
2 2 2 10
3 3 3 11
4 4 4 100
5 5 5 101
6 6 6 110
7 7 7 111
8 10 8 1000
9 11 9 1001
10 12 A 1010
11 13 B 1011
12 14 C 1100
13 15 D 1101
14 16 E 1110
15 17 F 1111
16 20 10 10000
17 21 11 10001
・コメント
今回もコピペすると、sample outputの形が崩れてしまったので、正しい形は以下のリンクを参照
2進数が一番桁数が大きいので、それに合わせて.rjust()をする際の桁数を決める。
10進数でnの2,8,16進数表記はそれぞれ
'{:b}'.format(n)
'{:o}'.format(n)
'{:x}'.format(n)
で得ることができる。
・URL
https://www.hackerrank.com/challenges/python-string-formatting/problem
Designer Door Mat
・問題
Mr. Vincent works in a door mat manufacturing company. One day, he designed a new door mat with the following specifications:
- Mat size must be X. ( is an odd natural number, and is times .)
- The design should have 'WELCOME' written in the center.
- The design pattern should only use
|
,.
and-
characters.
Sample Designs
Size: 7 x 21
---------.|.---------
------.|..|..|.------
---.|..|..|..|..|.---
-------WELCOME-------
---.|..|..|..|..|.---
------.|..|..|.------
---------.|.---------
Size: 11 x 33
---------------.|.---------------
------------.|..|..|.------------
---------.|..|..|..|..|.---------
------.|..|..|..|..|..|..|.------
---.|..|..|..|..|..|..|..|..|.---
-------------WELCOME-------------
---.|..|..|..|..|..|..|..|..|.---
------.|..|..|..|..|..|..|.------
---------.|..|..|..|..|.---------
------------.|..|..|.------------
---------------.|.---------------
Input Format
A single line containing the space separated values of and .
Constraints
Output Format
Output the design pattern.
Sample Input
9 27
Sample Output
------------.|.------------
---------.|..|..|.---------
------.|..|..|..|..|.------
---.|..|..|..|..|..|..|.---
----------WELCOME----------
---.|..|..|..|..|..|..|.---
------.|..|..|..|..|.------
---------.|..|..|.---------
------------.|.------------
・コメント
ドアマット(?)を設計する問題。コピペでドアマットの形が崩れてしまったので、正しい形は以下のURL参照。
ドアマットの形に着目して、.|.が何個存在するかを考え、前回の問題で登場した.centerを用いて中央に配置する。
例などから分かるように、真ん中の行を挟んで対象なので、その性質を用いている。
patternという配列があれば、それと並びが逆の配列は、pattern[::-1]という形で指定することができる。
・URL
https://www.hackerrank.com/challenges/designer-door-mat/problem
Text Wrap
・問題
You are given a string and width .
Your task is to wrap the string into a paragraph of width .
Input Format
The first line contains a string, .
The second line contains the width, .
Constraints
Output Format
Print the text wrapped paragraph.
Sample Input 0
ABCDEFGHIJKLIMNOQRSTUVWXYZ
4
Sample Output 0
ABCD
EFGH
IJKL
IMNO
QRST
UVWX
YZ
・コメント
与えられた長い文字列を、与えられた文字数ごとに改行する問題。
textwrap.wrap(string, max_width)
でstringをmax_widthごとに区切り、リストに入れることができる。
これをjoin()を用いて改行しながら出力すれば答えが得られる。
・URL
Text Alignment
・問題
In Python, a string of text can be aligned left, right and center.
.ljust(width)
This method returns a left aligned string of length width.
>>> width = 20
>>> print 'HackerRank'.ljust(width,'-')
HackerRank----------
.center(width)
This method returns a centered string of length width.
>>> width = 20
>>> print 'HackerRank'.center(width,'-')
-----HackerRank-----
.rjust(width)
This method returns a right aligned string of length width.
>>> width = 20
>>> print 'HackerRank'.rjust(width,'-')
----------HackerRank
Task
You are given a partial code that is used for generating the HackerRank Logo of variable thickness.
Your task is to replace the blank (______
) with rjust, ljust or center.
Input Format
A single line containing the thickness value for the logo.
Constraints
The thickness must be an odd number.
Output Format
Output the desired logo.
Sample Input
5
Sample Output
H
HHH
HHHHH
HHHHHHH
HHHHHHHHH
HHHHH HHHHH
HHHHH HHHHH
HHHHH HHHHH
HHHHH HHHHH
HHHHH HHHHH
HHHHH HHHHH
HHHHHHHHHHHHHHHHHHHHHHHHH
HHHHHHHHHHHHHHHHHHHHHHHHH
HHHHHHHHHHHHHHHHHHHHHHHHH
HHHHH HHHHH
HHHHH HHHHH
HHHHH HHHHH
HHHHH HHHHH
HHHHH HHHHH
HHHHH HHHHH
HHHHHHHHH
HHHHHHH
HHHHH
HHH
H
・コメント
よくわからないロゴを作る問題。コピペして貼り付けたらロゴが崩れてしまったので、正しい形は以下のURLを参照。
Discussionでも、こんな問題やる意味あんのか?と書かれていたので、モチベはかなり低め。
.ljust(width),.center(width),.rjust(width)
の3つの挙動さえ分かればそれ以上やる意味なさそう......
3つの挙動については問題文を見てね
・URL
https://www.hackerrank.com/challenges/text-alignment/problem
String Validators
・問題
Python has built-in string validation methods for basic data. It can check if a string is composed of alphabetical characters, alphanumeric characters, digits, etc.
str.isalnum()
This method checks if all the characters of a string are alphanumeric (a-z, A-Z and 0-9).
>>> print 'ab123'.isalnum()
True
>>> print 'ab123#'.isalnum()
False
str.isalpha()
This method checks if all the characters of a string are alphabetical (a-z and A-Z).
>>> print 'abcD'.isalpha()
True
>>> print 'abcd1'.isalpha()
False
str.isdigit()
This method checks if all the characters of a string are digits (0-9).
>>> print '1234'.isdigit()
True
>>> print '123edsd'.isdigit()
False
str.islower()
This method checks if all the characters of a string are lowercase characters (a-z).
>>> print 'abcd123#'.islower()
True
>>> print 'Abcd123#'.islower()
False
str.isupper()
This method checks if all the characters of a string are uppercase characters (A-Z).
>>> print 'ABCD123#'.isupper()
True
>>> print 'Abcd123#'.isupper()
False
Task
You are given a string .
Your task is to find out if the string contains: alphanumeric characters, alphabetical characters, digits, lowercase and uppercase characters.
Input Format
A single line containing a string .
Constraints
Output Format
In the first line, print True
if has any alphanumeric characters. Otherwise, print False
.
In the second line, print True
if has any alphabetical characters. Otherwise, print False
.
In the third line, print True
if has any digits. Otherwise, print False
.
In the fourth line, print True
if has any lowercase characters. Otherwise, print False
.
In the fifth line, print True
if has any uppercase characters. Otherwise, print False
.
Sample Input
qA2
Sample Output
True
True
True
True
True
・コメント
なし、問題文読んで
・URL
https://www.hackerrank.com/challenges/string-validators/problem
Find a string
・問題
In this challenge, the user enters a string and a substring. You have to print the number of times that the substring occurs in the given string. String traversal will take place from left to right, not from right to left.
NOTE: String letters are case-sensitive.
Input Format
The first line of input contains the original string. The next line contains the substring.
Constraints
Each character in the string is an ascii character.
Output Format
Output the integer number indicating the total number of occurrences of the substring in the original string.
Sample Input
ABCDCDC
CDC
Sample Output
2
Concept
Some string processing examples, such as these, might be useful.
There are a couple of new concepts:
In Python, the length of a string is found by the function len(s)
, where is the string.
To traverse through the length of a string, use a for loop:
for i in range(0, len(s)):
print (s[i])
A range function is used to loop over some length:
range (0, 5)
Here, the range loops over to . is excluded.
・コメント
一行目の入力内に二行目の入力が何個含まれているかを調べる問題。
問題文中で出されている例として、ABCDCDCの中にCDC
は2つ含まれているというものがあった。前側のCDCの最後のCと後ろ側のCDCの最初のCは被っているという点に気をつけなければならない。
この問題を解く際に、共通する文字列がどれだけ含まれているかを出力する関数を探したが、この重複を許さないものしか見つけることができなかった(もっと探せばあったのかも?)ので、自分で真面目に書くことにした。
例えば例ではABCDCDCという7文字の中からCDCという3文字を見つけるので、7文字の中から連続した3文字の抜き取り方はABC,BCD,CDC,DCD,CDCの5通りしかないので、これらが一致する個数をカウントすればいいだけと気づいたので、これを一般的な形に拡張した。
一行目の文字列の長さをL1,二行目の文字列の長さをL2とすると長さL1の文字列から長さL2の文字列を抜き取る方法はL1-L2+1通りあるので、その全てで一致するかを試す。
文字列stringのk番目からn番目までを抜き出す方法はstring[k-1:k+n]でよい。
・URL