exercism / uiua

Exercism exercises in Uiua.
https://exercism.org/tracks/uiua
MIT License
2 stars 5 forks source link

Implement exercises #7

Open ErikSchierboom opened 2 weeks ago

ErikSchierboom commented 2 weeks ago

Let's use this issue to claim exercises we want to add.

ErikSchierboom commented 2 weeks ago

I'd like to do largest-series-product

vaeng commented 2 weeks ago

I'll start easy and try leap

ErikSchierboom commented 2 weeks ago

That's actually not that easy, but there is a video to help out: https://www.youtube.com/watch?v=H7GJbSHgWjw&ab_channel=TacitSanctuary

SleeplessByte commented 2 weeks ago
/≠=0◿4_100_400 <input>

Apparently.

From the comment:

It's a variadic exclusive or. It's only true if an odd number of inputs are true; it fits here since each condition is a subset of the prior and reverses the result. xor can also be thought of as a conditional toggle (a xor b = if b then not a else a).

Always great when kagi gives no real results, but once I clicked / (reduce) + (not equals) + = (equals) + (modulus) + _ (array syntax), and then cried a little, I understand.

◿2_3_4_10_100 10_10_10_10_10

# equivalent
◿2_3_4_10_100 10

# returns
[0 1 2 0 10]

So first give me an array that is the remainder of input / value:

◿4_100_400 <input>

For each item, does it compare to 0 (aka is it divisible?)

=0◿4_100_400 2100
[1 1 0]

=0◿4_100_400 2024
[1 0 0]

=0◿4_100_400 2017
[0 0 0]

But with reduce, the function not equals is called against the previous value.

\≠=0◿4_100_400 2100
[1 0 0]

\≠=0◿4_100_400 2024
[1 1 1]

\≠=0◿4_100_400 2017
[0 0 0]

You can compare the answers (steps) here with the previous each result. The first item in the array should always be the same. Then, in the reduce against not equals, the value will be a 1 if the previous result does not match the current one.

[1 -> 1] these are the same so, 0 [1 -> 0] these are different, so 1

In the 2100 case, what happens is:

◿4_100_400 2100
[0 0 100]

=0◿4_100_400 2100
[1 1 0]

# In other words
2100 % 4   => 0    =>   0 == 0   => 1
2100 % 100 => 0    =>   0 == 0   => 1
2100 % 400 => 100  => 100 == 0   => 0

The first value (1) is the initial reduced value [1]
The second value (1) is compared to the result (1). 1 ≠ 1 => 0
The last value (0) is compared to the result (0) 0 ≠ 0 => 0

Therefore: not leap.

The 2024 case:

◿4_100_400 2024
[0 24 24]

=0◿4_100_400 2024
[1 0 0]

# In other words
2024% 4   => 0    =>   0 == 0   => 1
2024% 100 => 24   =>  24 == 0   => 0
2024% 400 => 24   =>  24 == 0   => 0

The first value (1) is the initial reduced value [1]
The second value (1) is compared to the result (0). 1 ≠ 0 => 1
The last value (0) is compared to the result (1) 0 ≠ 1 => 1

Therefore: leap.

It works because for each division the result (1 or 0) needs to be different from the previous, and you need an odd number of 1 (divisible by x) for the entire thing to be 1.

I can now rest in peace(s)

Playground (pad): https://www.uiua.org/pad?src=0_14_0-dev_2__TGVhcCDihpAgL-KJoD0w4pe_NF8xMDBfNDAwCgriiLVMZWFwIFsyMDAwIDIwMDEgMjAwNCAyMDE3IDIwMjQgMjEwMF0KIyBleHBlY3RlZDogWzEgMCAxIDAgMSAwXQo=

vaeng commented 2 weeks ago

I think we found our first deep -dive 🎉🥳

SleeplessByte commented 2 weeks ago

Here are three more options:

The sum of "which are divisible" must be an odd number

I think, if my thought process is right, then the following is equivalent

=1◿2/+=0◿4_100_400 <input>

If each divisible item is counted as a 1 and each item that yields a remainder is 0, then the sum of all the 1 and 0 gives us an odd number when it's leap and even number when it's not.

Filter array for 1 (indicating divisibility) and get the length of the array

Alternatively, one can filter all the items for 1s (▽ =1.) and then count () the 1s

⧻▽ =1. <array> is always equivalent to /+ in this context because filtering for 1 and then getting the length, is the same as summing all the 1.

=1◿2⧻▽ =1. =0◿4_100_400 <input>

Simplify that

But then we don't need the earlier equality comparison and can count what's divisible exactly:

◿2⧻▽ =0. ◿4_100_400 <input>
ErikSchierboom commented 2 weeks ago

Fantastic writeup! I'm happy for you to join in adding some exercises :) (if you'd like and have time)

SleeplessByte commented 2 weeks ago

Ok. One more and then I need to go do something else. This is way too interesting.

Here is big brain mode:

=⊢↘ 1 datetime °datetime [<input> 2 29] 2

Or alternatively written:

=2⊢↘1 datetime °datetime [<input> 2 29]

And yes =29⊢↘2 works for the same reason.

If we need to turn this into a function, a bit of reshuffling is necessary.

Reordering the stack

Okay, the input is not at the end, so it cannot be "inserted" from the top, yay. But that can be fixed using:

The date we want looks like [<input> 2 29] (which is year-2-29). Therefore the input needs to be reordered

Reorder ← [⊃(⋅⋅∘|⋅∘|∘)]
Leap ← =2⊢↘1 datetime °datetime [⊃(⋅⋅∘|⋅∘|∘)] 29 2

∵Leap[2000 2001 2004 2017 2024 2100]
# expected: [1 0 1 0 1 0]

# [⊃(⋅⋅∘|⋅∘|∘)] 29 2 2000
# => [2000 ⊃(⋅⋅∘|⋅∘)]
# => [2000 2 ⊃(⋅⋅∘)]
# => [2000 2 29]
ErikSchierboom commented 2 weeks ago

I'll do isogram

SleeplessByte commented 2 weeks ago

@vaeng let me know when you have something. I'm happy to write all of the above as approaches.

vaeng commented 2 weeks ago

@SleeplessByte

=1◿2/+=0◿4_100_400

It's my first dive into an array language, and this is what I came up with in #9, after I understood that I had an array in an array and thus needed deshaping. Fixed that, thank you

SleeplessByte commented 2 weeks ago

=1 can always be dropped, so yeah nice! You basically did the condensed version of the first solution in https://github.com/exercism/uiua/issues/7#issuecomment-2471454928

https://github.com/exercism/uiua/issues/7#issuecomment-2471474194 still my best one though.

I'll start writing deep dive docs

ErikSchierboom commented 2 weeks ago

I'll do difference-of-squares

ErikSchierboom commented 2 weeks ago

@vaeng I've done some course regex replaces on the 8th track's exercise (which is a stack-based language) to create Uiua test files. They'll not be perfect, but they are a good starting point. You can find the branch here: https://github.com/exercism/8th/tree/uiua

ErikSchierboom commented 2 weeks ago

I'll do darts and hamming

vaeng commented 2 weeks ago

@vaeng I've done some course regex replaces on the 8th track's exercise (which is a stack-based language) to create Uiua test files. They'll not be perfect, but they are a good starting point. You can find the branch here: https://github.com/exercism/8th/tree/uiua

That will speed things up, thanks!

ErikSchierboom commented 2 weeks ago

I'll take pangram

ErikSchierboom commented 2 weeks ago

I'll work on eliuds-eggs and collatz-conjecture.

vaeng commented 2 weeks ago

I'll try acronym. Let's see some string magic.

vaeng commented 2 weeks ago

Acronym was okay, let's see how Bob goes.

glaxxie commented 2 weeks ago

Let me start with something simple like resistor color first I still need to set this up properly, it doesnt seem like it likes window much (at least on my end)

glaxxie commented 2 weeks ago

Done, however I was too hasty in declare "resistor color" as simple.

SleeplessByte commented 2 weeks ago

I am halfway done writing the approaches. Maybe I'll do an exercise then.

In the meanwhile one of my colleagues solved leap... interestingly

glaxxie commented 1 week ago

I've done the first 3 exercises of the resistor color series and PR them. The example solution might not be the best or clear idiomatic, but it has been fun so far

ErikSchierboom commented 1 week ago

The example solution might not be the best or clear idiomatic, but it has been fun so far

Neither are mine. That's totally fine! Thanks for the PRs.

ErikSchierboom commented 1 week ago

In the meanwhile one of my colleagues solved leap... interestingly

That's definitely worthy of an approach! 😮

ErikSchierboom commented 1 week ago

I'll do raindrops and reverse-string

ErikSchierboom commented 1 week ago

Whilst reading the docs, I came across the fact that you can document a function's parameters via comments: https://www.uiua.org/tutorial/documentation This might be useful for stubs. What do you all think?

I'm not sure about giving the return value a name 🤷

vaeng commented 1 week ago

I'm not sure about giving the return value a name

The built-in function's tooltips give something like dip: Temporarily pop ..., so I would keep it that way. The naming of return value(s) and parameters looks useful as a more relatable way to understand the signature of a function

vaeng commented 1 week ago

I'll do gigasecond

ErikSchierboom commented 1 week ago

@vaeng Let me know what you think: https://github.com/exercism/uiua/pull/29

ErikSchierboom commented 1 week ago

I'll do grains and rna-transcription

ErikSchierboom commented 1 week ago

And armstrong-numbers and nucleotide-count

vaeng commented 1 week ago

I'll do hexadecimal next, if it weren't deprecated. isbn-verifier it is.

glaxxie commented 1 week ago

I'll do rotational cipher

ErikSchierboom commented 1 week ago

I'll do square-root and sum-of-multiples

vaeng commented 1 week ago

21 exercises in two days. What an exciting language!

ErikSchierboom commented 1 week ago

I'm so loving it!

glaxxie commented 1 week ago

Finished the rotational cipher ! I was stuck for quite some time despite having the right idea, finally discover the keyword switch and things just click into places. End up quite happy with the final result. Imma take a closer look at the language tour and the doc before picking up another new exercise, you guys have fun in the mean time! Edit: also please add resistor-color-trio to the master list at the top as well, thank you Erik :smiley:

vaeng commented 1 week ago

Edit: also please add resistor-color-trio to the master list at the top as well, thank you Erik 😃

Done.

ErikSchierboom commented 1 week ago

I'll do scrabble-score

ErikSchierboom commented 1 week ago

I'll take roman-numerals

glaxxie commented 1 week ago

Imma do anagram and resistor expert

ErikSchierboom commented 1 week ago

resistor expert? I don't know that exercise

ErikSchierboom commented 1 week ago

I'll do flatten-array

glaxxie commented 1 week ago

resistor expert? I don't know that exercise

It's this one I just remember that this somehow doesn't exist on the problem-specifications, was it purged at some point or wasit python exclusive? I think I ported this from python to powershell manually as well since it couldnt be pulled from the specs

ErikSchierboom commented 1 week ago

Superficially it looks like resistor-color-trio but with one added suffix. If so, I don't think it's that interesting but 🤷

glaxxie commented 1 week ago

Yeah, trio to expert didn't really have a distinct feel compare to trio to duo. Imma just do anagram and find something later

ErikSchierboom commented 1 week ago

I'll take allergies

ErikSchierboom commented 1 week ago

And diamond