我最近开始打密码,试着写一个最小的拉紧图检查程序。
重言式是一个句子,在这个句子中,所有的单词都以同一个字母开头,例如:法国的鲜花盛开。
给定一个句子作为输入,判断它是否是重言之词。
Flowers flourish from France
True
This is not a Tautogram
False我提出了这个python代码(因为它是我的主要语言):
print(True if len(list(set([x.upper()[0] for x in __import__('sys').argv[1:]]))) == 1 else False)用法:
python3 tautogram.py Flowers flourish from France
# True
python3 tautogram.py This is not a Tautogram
# False句子可能包含逗号和句号,但没有其他特殊字符,只有大写和小写字母和空格。
它的大小是98字节。在任何语言中有更小的解决方案吗?
发布于 2019-03-25 20:18:56
发布于 2019-03-25 19:59:42
发布于 2019-03-25 22:12:03
在网上试试!。但是,TIO不支持Clojure的标准字符串库,因此第一个版本将抛出一个“无法找到小写”错误。
(fn[s](use '[clojure.string])(apply =(map first(map lower-case(split s #" ")))))未高尔夫球:
(defn tautogram? [s]
(use '[clojure.string])
(->> (split s #" ") ; Get words
(map lower-case)
(map first) ; Get first letter of each word
(apply =))) ; And make sure they're all the same我制作了一个避免导入的版本:
(fn [s](apply =(map #(if(<(-(int %)32)65)(int %)(-(int %) 32))(map first(take-nth 2(partition-by #(= %\ )s))))))
; -----
(defn tautogram? [s]
(->> s
(partition-by #(= % \ )) ; Split into words
(take-nth 2) ; Remove spaces
(map first) ; Get first letter
; Convert to uppercased letter code
(map #(if (< (- (int %) 32) 65) ; If attempting to uppercase puts the letter out of range,
(int %) ; Go with the current code
(- (int %) 32))) ; Else go with the uppercased code
(apply =))) ; And check if they're all equal但它是112字节。
https://codegolf.stackexchange.com/questions/182175
复制相似问题