Why was 6 afrhelp of 7? Due to the fact that 7 8 9!

Given a string use the complying with transformations:

If tbelow is a 6 alongside a 7 remove the 6 (6 is afrhelp of 7)If the sequence "789" appears remove the 8 and also the 9 (7 ate 9)

(If I"m not mistaken it doesn"t matter what order you do the revolutions in)

Keep using these transformations until you deserve to no longer.

You are watching: Why was 6 afraid of 7?

Example:

78966

First we check out "789", so the string becomes "766". Then we watch "76", so we take out the 6, and the string becomes "76". Then we view "76" aget, so we are left with "7".

Test Cases:

987 => 987 (Not in the best order. Does nothing.)6 7 => 6 7 (The whiteroom acts as a buffer between 6 and 7. Nothing happens)676 => 7 7896789 => 777689 => 7abcd => abcd
code-golf string
Share
Improve this question
Follow
asked Dec 14 "15 at 18:06

geokavelgeokavel
6,43633 gold badges2121 silver badges5959 bronze badges
$endgroup$
14
| Show 9 more comments

28 Answers 28


Active Oldest Votes
31
$egingroup$
Retina, 12Translation of the sed answer:

6*7(6|89)*7Try it online


Share
Improve this answer
Follow
edited Jun 17 "20 at 9:04
*

Community♦
1
answered Dec 14 "15 at 19:02
*

Digital TraumaDigital Trauma
68.4k88 gold badges100100 silver badges245245 bronze badges
$endgroup$
1
Add a comment |
12
$egingroup$
Javascript ES6, 29 bytess=>s.replace(/6*7(89|6)*/g,7)Test:

f=s=>s.replace(/6*7(89|6)*/g,7);`987 -> 9876 7 -> 6 7676 -> 77896789 -> 777689 -> 7abcd -> abcd`.split` `.every(t=>(t=t.split` -> `)&&f(t<0>)==t<1>)
Share
Improve this answer
Follow
edited Dec 14 "15 at 21:34

*

lirtosiast
20.9k55 gold badges4848 silver badges121121 bronze badges
answered Dec 14 "15 at 21:20
*

QwertiyQwertiy
2,89911 gold badge88 silver badges2828 bronze badges
$endgroup$
1
Add a comment |
12
$egingroup$
Java, 126 81 66 58 bytesThanks to
GamrCorps for giving the lambda variation of this code!

Thanks to
user902383 for mentioning an autoboxing trick!

...yup.

It"s actually much longer than I meant - Java reareas items in strings with replaceAll() as soon as per match, not continuously till it stops transforming. So I had to use a fancy for loop.

Lambda form:

x->789","7")););rerotate x;

Function form:

String s(String x)789","7")););rerevolve x;

Testable Ungolfed Code:

class B public static void main(String<>a) System.out.print(new B().s(a<0>)); String s(String x)for(;x!=(x=x.replaceAll("67
Share
Improve this answer
Follow
edited Dec 16 "15 at 16:36
answered Dec 14 "15 at 19:42
Addiboy CrumpAddikid Crump
10.9k11 gold badge3737 silver badges8484 bronze badges
$endgroup$
11
| Show 6 even more comments
9
$egingroup$
GNU Sed, 17Score has +1 for -r alternative.

s/6*7(6|89)*/7/g
Share
Improve this answer
Follow
edited Jun 17 "20 at 9:04
Community♦
1
answered Dec 14 "15 at 18:51
Digital TraumaDigital Trauma
68.4k88 gold badges100100 silver badges245245 bronze badges
$endgroup$
3
Add a comment |
8
$egingroup$
Perl 6, 19  18 bytes

89>*/7/ # 19 bytes

$ perl6 -pe "s:g/6*7<6|89>*/7/" # 17 + 1 = 18 bytes( Keep in mind that <6|89> is the non-recording variation of (6|89) which is spelt as (?:6|89) in Perl 5. is just how you would write what"s spelt as <6|89> in Perl 5)

usage:

$ perl6 -pe "s:g/6*7<6|89>*/7/" 9876 77777abcd6897779|689
Share
Improve this answer
Follow
edited Jun 2 "16 at 20:55
msh210
3,1231717 silver badges3434 bronze badges
answered Dec 14 "15 at 18:35
Brad Gilbert b2gillsBrad Gilbert b2gills
12.8k11 gold badge1515 silver badges3333 bronze badges
$endgroup$
6
| Sjust how 1 even more comment
7
$egingroup$
Pyth, 17 bytesu:G"67|76|789"7zTry it here.

Leaky Nun has actually outgolfed this by a byte in the comments.


Share
Improve this answer
Follow
edited Jun 17 "20 at 9:04
Community♦
1
answered Dec 14 "15 at 18:37
Mike BufardeciMike Bufardeci
1,71088 silver badges2121 bronze badges
$endgroup$
1
Add a comment |
4
$egingroup$
Perl 5, 17 bytesperl -pe "s/6*7(6|89)*/7/g" # 16 + 1usage:

$ perl -pe "s/6*7(6|89)*/7/g" 9876 77777abcd68977
Share
Improve this answer
Follow
answered Dec 14 "15 at 19:49
Brad Gilbert b2gillsBrad Gilbert b2gills
12.8k11 gold badge1515 silver badges3333 bronze badges
$endgroup$
Add a comment |
4
$egingroup$

Mathematica, 52 bytes

StringReplace<#,"67"|"76"|"789"->"7">&~FixedPoint~#&Explanation:

& A feature returning & a duty returning # its initially argumentStringReplace< , > with "67" "67" | or "76" "76" | or "789" "789" -> replaced via "7" "7" ~FixedPoint~ used to # its first argument until it no longer transforms.
Share
Improve this answer
Follow
answered Dec 14 "15 at 22:10
LegionMammal978LegionMammal978
17.6k44 gold badges2121 silver badges5555 bronze badges
$endgroup$
3
Add a comment |
3
$egingroup$

Rust, 96 bytes

fn f(mut s:String)->Stringfor _ in 0..s.len()for r in&<"67","76","789">s=s.replace(r,"7")sHopelessly lengthy, as per usual for Rust...

Ungolfed:

fn seven_ate_nine(mut str: String) -> String for _ in 0..str.len() for to_rearea in &<"67","76","789"> str = str.replace(to_replace, "7"); s
Share
Improve this answer
Follow
answered Dec 14 "15 at 22:30
Doorknob♦Doorknob
69.8k1919 gold badges134134 silver badges375375 bronze badges
$endgroup$
1
$egingroup$ At leastern it's not Java $endgroup$
–user45510
Dec 17 "15 at 2:49
Add a comment |
3
$egingroup$
Emacs Lisp, 59 bytes(lambda(s)(replace-regexp-in-string"6*7\(6\|89\)*""7"s))It becomes a little clearer through spaces:

(lambda (s) (replace-regexp-in-string "6*7\(6\|89\)*" "7" s))
Share
Improve this answer
Follow
answered Dec 14 "15 at 22:35
Lord YuumaLord Yuuma
58722 silver badges22 bronze badges
$endgroup$
Add a comment |
3
$egingroup$
Ruby, 27 bytesThis solution is from comments, credit to Brad Gilbert b2gills.

->s89)*/,?7

Ruby, 37 bytes

(old solution)

This solution uses the truth that you will certainly never before have to relocation more times than personalities in the string.

->ss.chars789/,?7;s
Share
Improve this answer
Follow
edited Jun 17 "20 at 9:04
Community♦
1
answered Dec 14 "15 at 21:48
MegaTomMegaTom
4,09611 gold badge1717 silver badges2424 bronze badges
$endgroup$
6
| Sjust how 1 more comment
2
$egingroup$
Japt, 15 bytesUr"6*7(89|6)*"7Simple RegEx solution

Try it online


Share
Improve this answer
Follow
answered Dec 15 "15 at 1:55
DowngoatDowngoat
28.1k66 gold badges7676 silver badges152152 bronze badges
$endgroup$
Add a comment |
2
$egingroup$
PowerShell, 27 bytes$args-replace"6*7(89|6)*",7e.g.PS C: emp> .ate.ps1 "7689"7PS C: emp> .ate.ps1 "abcd"abcdPS C: emp> .ate.ps1 "68978966897896"68977Making use of:

someone else"s regex patternthe method -relocation does an international relocation by default in PowerShellloop unrolling, wbelow it will certainly apply the -regex operator to the array $args by using it to all the elements individually, and also there"s just one element right here because there"s only one manuscript parameter, so it works OK and we deserve to stop having to index facet <0>.

Novelty previous attempt prior to realising an international replace would do it; 74 bytes of building a chain of "-relocation -rearea -replace" using string multiplication, as many kind of times as the size of the string, then eval()ing it:

""$($args)""+("06|6(?=7)"089""-f"-replace"(?(With a little of string substitution to shorten the number of replaces).


Share
Improve this answer
Follow
edited Dec 15 "15 at 21:48
answered Dec 15 "15 at 21:42
TessellatingHecklerTessellatingHeckler
3,0121313 silver badges1818 bronze badges
$endgroup$
Add a comment |
2
$egingroup$

Explanation

j % input stringt % duplicate" % for each character. Iterates as many times as the string size "789|76" % consistent expression for replacement 55c % string to insert instead: character "7" YX % regexprep> % finish forThis functions by applying a continual expresion replacement for as many times as there are personalities in the original string. This is sufficient, considering that each substitution reduces the variety of characters.


Share
Improve this answer
Follow
edited Jun 1 "16 at 22:35
answered Dec 14 "15 at 22:21
Luis MendoLuis Menexecute
96.8k99 gold badges115115 silver badges344344 bronze badges
$endgroup$
Add a comment |
1
$egingroup$

Seriously, 29 bytes

,;l`"7;;"67"(Æ"76"(Æ"789"(Æ`nTakes input as a double-quoted string, prefer "6789". Try it online (you will certainly need to manually quote the input).

Explanation:

,;l`"7;;"67"(Æ"76"(Æ"789"(Æ`n,;l acquire input and push its size (we"ll contact it n) ` `n speak to the following function n times: "7;;"67"(Æ replace all cases of "67" through "7" "76"(Æ relocation all cases of "76" via "7" "789"(Æ replace all cases of "789" via "7"
Share
Improve this answer
Follow
edited Jun 17 "20 at 9:04
Community♦
1
answered Dec 15 "15 at 4:16
user45941user45941
$endgroup$
Add a comment |
1
$egingroup$

PHP, 36 bytes

preg_replace("/6*7(6|89)*/","7",$a);regex solution, takes $a string and also reareas via the expression.


Share
Improve this answer
Follow
edited Jun 17 "20 at 9:04
Community♦
1
answered Dec 15 "15 at 23:23
daraemandaraeguy
1122 bronze badges
$endgroup$
4
$egingroup$
immibis Correct. A agreement is forced to make an I/O strategy acceptable. The lack of one suggests it is not acceptable. $endgroup$
–user45941
Dec 16 "15 at 20:10
Add a comment |
1
$egingroup$
Thue, 26 bytes67::=776::=7789::=7::=consisting of a trailing newline.

See more: I Am 14 Years Old In Spanish I Am 14 Years Old? How Do You Say In Spanish I Am 14 Years Old

Input is appfinished to the routine before starting it.Output is check out off the regimen state once it terminates, similarly to a Turing machine.(Thue does have an output stream, but it"s hard to usage effectively, so I"m not certain whether this is an acceptable output method)


Share
Improve this answer
Follow
edited Jun 17 "20 at 9:04
Community♦
1
answered Dec 16 "15 at 9:31
user253751user253751
90466 silver badges1414 bronze badges
$endgroup$
2
$egingroup$ I don't think so. If you have actually a means to STDOUT, you have to. Sorry! $endgroup$
–user45510
Dec 17 "15 at 2:48
Add a comment |
1
$egingroup$

CJam, 70 64 bytes

Thanks to
Peter Taylor for cutting "789":I"76:":I? to "789""76"?:I

"67":Iq:AAI#:B)AB+s:A>;"76"I="789":I"76":I?>;?/A

"67":Iq:AAI#:B)AB+s:A>;"76"I="789""76"?:I>;?/A

I recognize this might probably be golfed a lot better and your help would certainly be substantially appreciated, but frankly I"m simply happy I managed to acquire the answer. This was my first attempt at composing CJam.

Explanation:

"67":I e# Assign the value of 67 to Iq:A e# Read the input and also assign to A e# Opening brackets for loop AI#:B) e# Get the index of I inside A and also assign to B. The increment worth by 1 to usage for if condition (execute not want to process if the index was -1) e# Open brackets for true outcome of if statement AB e# Slice A to acquire every little thing after index B plus the size of string I (this will certainly remove I entirely) +s:A e# Append both slices, convert to string, and also assign earlier to A >; e# Clear the stack e# Cshedding brackets for the if condition e# Open brackets for false outcome of if statement "76"I= e# Check if I is equal to 76 "789" e# If I is 76, make I 789 "76"?:I e# If I is not 76, make I 76 >; e# Clear the stack if I does not exist inside A ? e# Closing brackets for false result of if statement/ e# LoopA e# Output A