mersenneforum.org

mersenneforum.org (https://www.mersenneforum.org/index.php)
-   jvang (https://www.mersenneforum.org/forumdisplay.php?f=153)
-   -   ¿Learning how to learn… (https://www.mersenneforum.org/showthread.php?t=23429)

R. Gerbicz 2018-09-28 11:25

[QUOTE=jvang;496941]
I expected this level 6 kata, [URL="https://www.codewars.com/kata/are-they-the-same/"]Are they the "same"?[/URL], to be harder, but it was pretty straightforward.

[CODE]module Codewars.Kata.Compare where
import Data.List
comp :: [Integer] -> [Integer] -> Bool
comp xs ys = if (sort (map (^2) xs)) == (sort ys) then True else False[/CODE]

However, no other solutions I saw used anything different; they all squared the elements of the first list and compared that to the second, which seems inefficient for large numbers and/or for large lists. I can't think of a better way to do so...
[/QUOTE]

If we assume that the numbers are small then actually the sorting is more costly, so you should eliminate that one, and actually there is a method for this:
[CODE]
module Codewars.Kata.Compare where
import Data.List
comp :: [Integer] -> [Integer] -> Bool
comp xs ys = if (sum (map (^14) xs))+fromIntegral (length xs)==(sum (map (^7) ys))+fromIntegral (length ys) then True else False
[/CODE]
In most of the cases this is giving the correct answer.

jvang 2018-09-29 22:34

[QUOTE=Nick;496970]It doesn't need to know that: the takeWhile stops at the first zero in the list.

Yes, that works, and the final line can be made even simpler:
[CODE]
comp xs ys = (sort (map (^2) xs)) == (sort ys)
[/CODE][/QUOTE]

Oh yeah....oops for both comments :buddy:

[QUOTE=R. Gerbicz;496973]If we assume that the numbers are small then actually the sorting is more costly, so you should eliminate that one, and actually there is a method for this:
[CODE]
module Codewars.Kata.Compare where
import Data.List
comp :: [Integer] -> [Integer] -> Bool
comp xs ys = if (sum (map (^14) xs))+fromIntegral (length xs)==(sum (map (^7) ys))+fromIntegral (length ys) then True else False
[/CODE]
In most of the cases this is giving the correct answer.[/QUOTE]

That's a really neat way to approach the problem without sorting it... it's kind of like taking a rudimentary checksum of the two lists (maybe?)!

I did an interesting level 5 kata, [URL="https://www.codewars.com/kata/human-readable-time/"]Human Readable Time[/URL]. It turned out to be pretty simple compared to other level 5 katas, though I had a bit of trouble with [C]show[/C] and [C]++[/C] (I kept trying to use [C]concat[/C] at first, then I was trying to append [C]':'[/C] instead of [C]":"[/C]).

[CODE]module HumanTime where

humanReadable :: Int -> String
humanReadable x = (add0 (h x)) ++ ":" ++ (add0 (m x)) ++ ":" ++ (add0 (s x))
add0 x = if x < 10 then '0':(show x) else show x
h x = div x 3600
m x = div (x - (h x) *3600) 60
s x = x - (h x) *3600 - (m x) *60[/CODE]

Nick 2018-09-30 08:42

[QUOTE=jvang;497104]I did an interesting level 5 kata, [URL="https://www.codewars.com/kata/human-readable-time/"]Human Readable Time[/URL]. It turned out to be pretty simple compared to other level 5 katas, though I had a bit of trouble with [C]show[/C] and [C]++[/C] (I kept trying to use [C]concat[/C] at first, then I was trying to append [C]':'[/C] instead of [C]":"[/C]).[/QUOTE]
Yes, that's correct, but you could simplify it by using mod as well as div!

jvang 2018-09-30 23:01

[QUOTE=Nick;497134]Yes, that's correct, but you could simplify it by using mod as well as div![/QUOTE]

Hmm, I suppose so! I'm much less comfortable working with [C]mod[/C] than I should be...

Came back to [URL="https://www.codewars.com/kata/weight-for-weight/"]Weight for weight[/URL], which gave me trouble a couple of weeks ago. I learned about [C]sortBy[/C]; this function applies a specified function, sorts the resulting list (or by whatever specified parameter), then puts each value of the original list in that order (keeping the order of duplicates in the modified list).

[CODE]module Codewars.G964.WeightSort where
import Data.Char
import Data.List
orderWeight :: [Char] -> [Char]
orderWeight string = unwords (sortOn weight (sort (words string)))
weight x = sum (map digitToInt x)[/CODE]

Pretty useful function; the Hoogle page used [C]fst[/C] as an example, for sorting tuples.

jvang 2018-10-01 22:49

Did some work with a new function, which helped me learn a bit with tuples. This was a level 6 kata, [URL="https://www.codewars.com/kata/find-the-parity-outlier/"]Find The Parity Outlier[/URL].

[CODE]module Kata where
import Data.List
findOutlier :: [Int] -> Int
findOutlier xs = evenOrOdd (partition even xs)
evenOrOdd (x,y) = if length x == 1 then head x else head y[/CODE]

[C]partition[/C] is the new function; this takes a Boolean function and returns a tuple of length 2. The first element is a list of all of the elements that satisfy the predicate, with the rest of the elements in the second list. This function made the kata pretty easy, since once I figured that out all I needed to do was work with the resulting tuple :jvang:

Nick 2018-10-03 08:49

[QUOTE=jvang;497158]Hmm, I suppose so! I'm much less comfortable working with [C]mod[/C] than I should be...
[/QUOTE]
For an integer n, if we know what n mod 10 is (or 'mod n 10" in Haskell),
i.e. we know its last digit in decimal, then we can work out what n mod 5 and n mod 2 are.
The cool thing is that this also works backwards: if someone tells you the values of
n mod 5 and n mod 2, then you can calculate what n mod 10 must have been.

[CODE]
n mod 10 n mod 5 n mod 2
0 0 0
1 1 1
2 2 0
3 3 1
4 4 0
5 0 1
6 1 0
7 2 1
8 3 0
9 4 1
[/CODE]This works because gcd(5,2)=1.
It's a very old piece of mathematics, known in China at least 700 years ago.
Today, we still call it the Chinese Remainer Theorem (or CRT).

jvang 2018-10-04 23:29

[QUOTE=Nick;497278]For an integer n, if we know what n mod 10 is (or 'mod n 10" in Haskell),
i.e. we know its last digit in decimal, then we can work out what n mod 5 and n mod 2 are.
The cool thing is that this also works backwards: if someone tells you the values of
n mod 5 and n mod 2, then you can calculate what n mod 10 must have been.

This works because gcd(5,2)=1.
It's a very old piece of mathematics, known in China at least 700 years ago.
Today, we still call it the Chinese Remainer Theorem (or CRT).[/QUOTE]

Ooh, that's cool! It sounds vaguely familiar too...

I made use of [C]nub[/C], which I learned about a while back. It keeps the first occurrence of all elements in a list, discarding all duplicates coming afterwards.

This kata was [URL="https://www.codewars.com/kata/estimating-amounts-of-subsets/"]Estimating Amounts of Subsets[/URL]:

[CODE]module Kata (estSubsets) where
import Data.List
estSubsets :: Ord a => [a] -> Integer
estSubsets [] = 0
estSubsets n = 2^ (length (nub n)) -1[/CODE]

Trial and error and messing with the test cases gave me a good idea of what the formula should be. I've never really had a class on probability in the past 5 years or so, and I can never remember simple things like the difference between combinations and permutations, how many of them you can get from a specific set, and how to do problems where I have 17 shirts, 20 pairs of shoes, and only 3 pairs of pants, and need all the unique combinations or something :ermm:

Nick 2018-10-05 09:07

[QUOTE=jvang;497387]Trial and error and messing with the test cases gave me a good idea of what the formula should be. I've never really had a class on probability in the past 5 years or so, and I can never remember simple things like the difference between combinations and permutations, how many of them you can get from a specific set, and how to do problems where I have 17 shirts, 20 pairs of shoes, and only 3 pairs of pants, and need all the unique combinations or something :ermm:[/QUOTE]
That's a great parody of a typical basic combinatorics problem! :smile:

You got the formula right in your program. Here's one way of thinking about it.
If we have a set S={a,b,c} then we can specify subsets of S using 3 binary digits:
the 1st digit is 1 if a is to be included in the subset and 0 otherwise,
the 2nd digit is 1 if b is to be included in the subset and 0 otherwise, and
the 3rd digit is 1 if c is to be included in the subset and 0 otherwise.
For example, 101 means "include a, don't include b, include c",
so it corresponds with the subset {a,c}.

Then each 3 digit binary number corresponds with a distinct subset of S,
and every possible subset of S has a 3 digit code representing it.
It follows that the number of subsets of S (including the empty set) equals the number of 3 digit binary numbers.
And as these are the numbers from 0 through 7, there are 8 of them.

jvang 2018-10-06 21:43

[QUOTE=Nick;497402]That's a great parody of a typical basic combinatorics problem! :smile:

You got the formula right in your program. Here's one way of thinking about it.
If we have a set S={a,b,c} then we can specify subsets of S using 3 binary digits:
the 1st digit is 1 if a is to be included in the subset and 0 otherwise,
the 2nd digit is 1 if b is to be included in the subset and 0 otherwise, and
the 3rd digit is 1 if c is to be included in the subset and 0 otherwise.
For example, 101 means "include a, don't include b, include c",
so it corresponds with the subset {a,c}.

Then each 3 digit binary number corresponds with a distinct subset of S,
and every possible subset of S has a 3 digit code representing it.
It follows that the number of subsets of S (including the empty set) equals the number of 3 digit binary numbers.
And as these are the numbers from 0 through 7, there are 8 of them.[/QUOTE]

That makes sense, but then why was the formula in the problem 2^x - 1 instead?

In the college thread I posted a bit about the supercomputing department at the University of Arkansas. Reading up on the access "nodes" (which seem relatively self-explanatory) got me thinking: what differentiates a supercomputer from an interconnected cluster of a couple thousand ordinary PCs, or a distributed computing project (barring data transfer speeds and latency)?

axn 2018-10-07 02:42

[QUOTE=jvang;497519]That makes sense, but then why was the formula in the problem 2^x - 1 instead?[/QUOTE]
We could tell you, but it is better that you "troubleshoot" this one yourself.

Nick 2018-10-07 09:49

[QUOTE=jvang;497519]In the college thread I posted a bit about the supercomputing department at the University of Arkansas. Reading up on the access "nodes" (which seem relatively self-explanatory) got me thinking: what differentiates a supercomputer from an interconnected cluster of a couple thousand ordinary PCs, or a distributed computing project (barring data transfer speeds and latency)?[/QUOTE]
It's a good question. Both do a lot of processing in parallel,
but in distributed systems the components are autonomous and less tightly coupled.
Two PCs attached to a network can be used independently or together.
When used together, they can communicate with each other via the network,
but they can't directly access each other's disks, memory, busses, caches, ...


All times are UTC. The time now is 10:06.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2021, Jelsoft Enterprises Ltd.