Skip to content

Instantly share code, notes, and snippets.

@wo0dyn
Last active March 17, 2023 11:04
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wo0dyn/3340763 to your computer and use it in GitHub Desktop.
Save wo0dyn/3340763 to your computer and use it in GitHub Desktop.
Write “KFC” with one-line-script using the string “Kentucky Fried Chicken” once.

KFC

Write KFC with one-line-script using the string “Kentucky Fried Chicken” once.

Bash

for w in $(echo "Kentucky Fried Chicken" | tr " " "\n"); do echo -n ${w:0:1}; done;

@xi-ao

echo "Kentucky Fried Chicken" | sed 's/[a-z ]//g'

@avernois

Clojure

(prn (clojure.string/replace "Kentucky Fried Chicken" #"[a-z ]" ""))
;; or 
(prn (apply str (map first (clojure.string/split "Kentucky Fried Chicken" #"\s"))))
;; or
(prn (apply str (filter #(and (not= \space %) (= (clojure.string/upper-case (str %)) (str %))) "Kentucky Fried Chicken")))
;; or
(prn (apply str (remove #(= \space %) (map first (partition-by #(= \space %) "Kentucky Fried Chicken")))))
;; or
(prn (apply str (for [x (seq "Kentucky Fried Chicken") :when (and (= (clojure.string/upper-case (str x)) (str x)) (not= \space x))] x)))
;; or
(prn (apply str (remove #(= \space %) (for [x (seq "Kentucky Fried Chicken") :when (= (clojure.string/upper-case (str x)) (str x))] x))))
;; or
(prn (apply str (re-seq #"[A-Z]" "Kentucky Fried Chicken")))
;; or
(prn (apply str (filter #(Character/isUpperCase %) "Kentucky Fried Chicken")))

@magopian

HTML

<style>span {display:none}</style>
<p>K<span>entucky </span>F<span>ried </span>C<span>hicken</span></p>

@n1k0 + @kud

Java

public class Kfc { public static void main(String[] args) { System.out.println(new String("Kentucky Fried Chicken").replaceAll("([^A-Z])", ""));}}

— Cyril

JavaScript (1.2)

print(Array.prototype.map.call("Kentucky Fried Chicken".split(' '), function(w) { return w[0] }).join(''))

@wo0dyn

Perl (5.10.1)

print "Kentucky Fried Chicken" =~ m/\b(\w)/g;

@jroussel

PHP (5.3)

<?php print join(array_map(function ($w){return $w[0];}, explode(' ', 'Kentucky Fried Chicken')));

@jroussel

Version un peu plus con⋅cise : 😁

<?php array_map(function($w) { print $w[0]; }, explode(' ', 'Kentucky Fried Chicken'));

@wo0dyn

<?php echo str_replace(' ', '', join(array_intersect(str_split($s = "Kentucky Fried Chicken"), str_split(strtoupper($s)))));

@xi-ao

<?php echo preg_replace('/[a-z]| /', '', "Kentucky Fried Chicken") ?>

@jeremyFreeAgent

<?php echo preg_replace('([^A-Z])', '', 'Kentucky Fried Chicken');

@SaniOKh

<?php foreach(explode(' ', "Kentucky Fried Chicken") as $w) echo $w[0];

@jeremyFreeAgent

Python (2.7.1+)

print ''.join(w[0] for w in 'Kentucky Fried Chicken'.split())
# or
import re; print re.sub('[^A-Z]', '', 'Kentucky Fried Chicken')
# or short-crappiest version ever (sorry!)
print 'KFC'#Kentucky Fried Chicken
# → yeah, we need more rules on that exercise…

@wo0dyn

print ''.join(c for c in "Kentucky Fried Chicken" if c.isupper())
# or
import re; print ''.join(re.findall(r'\b\w', 'Kentucky Fried Chicken'))
# or
import re; print ''.join(re.findall(r'[A-Z]', 'Kentucky Fried Chicken'))

@magopian

Ruby

print "Kentucky Fried Chicken".gsub /[^A-Z]/, ''
# or
"Kentucky Fried Chicken".each_char{|c| print(('Z' >= c && 'A' <= c)?c:'')}

— Cyril

@jeremyFreeAgent
Copy link

<?php echo preg_replace('/[a-z]| /', '', "Kentucky Fried Chicken") ?>

@xi-ao
Copy link

xi-ao commented Aug 13, 2012

# !/bin/bash

for w in $(echo "Kentucky Fried Chicken" | tr " " "\n"); do echo -n ${w:0:1}; done;

@wo0dyn
Copy link
Author

wo0dyn commented Aug 13, 2012

@jeremyFreeAgent Bon, j'ai ajouté mais l'idée à la base, c'était de récupérer les premières lettres de chaque mot hein ! Coquin.
Du coup, j'ai fait une autre version python pour la peine :

import re; print re.sub('[^A-Z]', '', 'Kentucky Fried Chicken')

@xi-ao Joli ! Ajouté.

@jeremyFreeAgent
Copy link

@duboisnicolas Du coup c'est pareil pour Perl ! ;)

@jeremyFreeAgent
Copy link

<?php foreach(explode(' ', "Kentucky Fried Chicken") as $w) { echo $w[0]; }; ?>

@wo0dyn
Copy link
Author

wo0dyn commented Aug 13, 2012

<?php preg_replace_callback('~\b(\w)~', function($w) { print $w[0]; }, 'Kentucky Fried Chicken');

@wo0dyn
Copy link
Author

wo0dyn commented Aug 13, 2012

@jeremyFreeAgent ajouté sans les accolades pour gagner quelques caractères. 😏

@jeremyFreeAgent
Copy link

Bien joué @duboisnicolas ! Je n'y avais pas pensé :s

@xi-ao
Copy link

xi-ao commented Aug 16, 2012

Sans utiliser les majuscules (mais en utilisant des regexp) par Cyril R. :

"Kentucky Fried Chicken".replace(/([a-z])[a-z]* ?/gi, '$1')

@wo0dyn
Copy link
Author

wo0dyn commented Aug 16, 2012

@xi-ao: quel langage ?

@n1k0
Copy link

n1k0 commented Jan 6, 2014

<?php
echo "KFC";
?>

ben quoi

@naholyr
Copy link

naholyr commented Jan 6, 2014

Ça marche pas @n1k0 : "using the string “Kentucky Fried Chicken” once."

Une solution un peu plus complète:

<?php
"Kentucky Fried Chicken";
echo "KFC";
?>

:D

@kud
Copy link

kud commented Jan 6, 2014

:') @naholyr 👍

@n1k0
Copy link

n1k0 commented Jan 6, 2014

Encore plus fort

<?php /* kfc.php */ ?>
<style>span {display:none}</style>
<p>K<span>entucky </span>F<span>ried </span>C<span>hicken</span></p>

@kud
Copy link

kud commented Jan 6, 2014

CA J'AIME @n1k0

Par contre, visibility: hidden, va laisser l'espacement alors que display: none, non.

@wo0dyn
Copy link
Author

wo0dyn commented Jan 6, 2014

@n1k0 @kud : c'est pour moi les copains → https://gist.github.com/wo0dyn/3340763#html

@n1k0
Copy link

n1k0 commented Jan 6, 2014

J'aime contribuer à des projets qui font avancer l'humanité.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment