首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >有更小的卷边检查器吗?

问有更小的卷边检查器吗?
EN

Code Golf用户
提问于 2019-03-25 19:23:45
回答 10查看 774关注 0票数 10

我最近开始打密码,试着写一个最小的拉紧图检查程序。

重言式是一个句子,在这个句子中,所有的单词都以同一个字母开头,例如:法国的鲜花盛开。

给定一个句子作为输入,判断它是否是重言之词。

测试用例

代码语言:javascript
复制
Flowers flourish from France
    True

This is not a Tautogram
    False

我提出了这个python代码(因为它是我的主要语言):

代码语言:javascript
复制
print(True if len(list(set([x.upper()[0] for x in __import__('sys').argv[1:]]))) == 1 else False)

用法:

代码语言:javascript
复制
python3 tautogram.py Flowers flourish from France
# True
python3 tautogram.py This is not a Tautogram
# False

句子可能包含逗号和句号,但没有其他特殊字符,只有大写和小写字母和空格。

它的大小是98字节。在任何语言中有更小的解决方案吗?

EN

回答 10

Code Golf用户

发布于 2019-03-25 20:18:56

05AB1E,5字节

代码语言:javascript
复制
l#€нË

在网上试试!

代码语言:javascript
复制
l      # Lowercase input.
 #     # Split on spaces.
  €н   # a[0] of each.
    Ë  # All equal?

这是在手机上做的借口,没有解释。

票数 7
EN

Code Golf用户

发布于 2019-03-25 19:59:42

Python 2,47字节

代码语言:javascript
复制
lambda s:len(set(zip(*s.lower().split())[0]))<2

在网上试试!

是在手机上想出来的。可能会得到更多的金牌。

票数 4
EN

Code Golf用户

发布于 2019-03-25 22:12:03

克洛尔,80字节

在网上试试!。但是,TIO不支持Clojure的标准字符串库,因此第一个版本将抛出一个“无法找到小写”错误。

代码语言:javascript
复制
(fn[s](use '[clojure.string])(apply =(map first(map lower-case(split s #" ")))))

未高尔夫球:

代码语言:javascript
复制
(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

我制作了一个避免导入的版本:

代码语言:javascript
复制
(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字节。

票数 3
EN
页面原文内容由Code Golf提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codegolf.stackexchange.com/questions/182175

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档