mstdn.games is one of the many independent Mastodon servers you can use to participate in the fediverse.
We are a gaming-focused space on Mastodon. We welcome everyone who enjoys any type of gaming - it doesn't just need to be video games. Let's build a diverse and inclusive community together!

Administered by:

Server stats:

465
active users

#programing

0 posts0 participants0 posts today

New era, new #introduction!

I started with #biology, realized I like #math along the road, and ended up loving #evolution.

After a BSc in the first, an MSc (and a half) in Bioinformatics/Computational biology (and applied math) I have started my #PhD in Ecological evolutionary modelling at @KorinnaAllhoff team at @unihohenheim!

I will be building models of biotic-interractions (antagonistic and/or mutualistic) between 2 and 3 species and check out emerging networks of these communities.

Looking forward to the adventure in this new landscape of possibilities of #programing#ecology#math!! :D

I am stupid-proud of this small #haskell program. It’s not complex, but it is pretty haskell-y.

It’s another HackerRank challenge: Given a string of lowercase characters – a “word” – find the very next word in the dictionary (lex sorted) made only by rearranging letters of your word OR “no answer” if that’s not possible.

module Main where

import Control.Monad (replicateM_)
import Data.List (find, sort, span)

go :: String -> String -> Maybe String
go ts "" = Nothing
go ts (h : hs) = do
    let ts' = sort ts
    let mc = find (> h) ts'
    case mc of
        Nothing -> go (h : ts) hs
        Just c -> do
            let (left, _ : right) = span (< c) (sort (h : ts'))
            Just $ reverse (c : left ++ right) ++ hs

solve :: String -> String
solve w = do
    maybe "no answer" reverse (go "" (reverse w))

main :: IO ()
main = do
    cases <- readLn :: IO Int
    replicateM_ cases $ do
        getLine >>= putStrLn . solve

It’s not necessarily the best or most efficient, but it does deal with haskell’s preference for the front of a list, light use of Maybe, recursion, … On a second pass, I might use Data.Sequence for working from the tail and splicing… still, it’s nice when concepts click. 🙂

nolineage.comAkkoma

I don’t know how I feel about #programing exercises that go a little far afield from programing… but I do like my math 🙂

Challenge from HackerRank, solution in #haskell

Given k in the natural numbers, N, and S subset of N such that the elements of S are unique, find max |S'| where S' subset of S and for all m, n in S' with m != n, k does not divide m + n.

module Main where

import qualified Data.IntMap as M

solve :: Int -> [Int] -> Int
solve 1 _ = 1
solve k xs =
    let
        -- size the mod k equivalence classes
        observed = M.fromListWith (+) $ map (\x -> (x `mod` k, 1)) xs
        -- for convenience in map lookups, zero any class not seen
        counts = foldr (\i acc -> M.insertWith (+) i 0 acc) observed [0 .. k - 1]
        -- two types of classes can only have at most one elt each
        picker :: Int -> Int -> Int
        picker m i
            | i == 0 || i == m - i = min 1 $ counts M.! i
            | i > m - i = 0  -- safety guard in case I screwed up the edge
            | otherwise = max (counts M.! i) (counts M.! (m - i))
     in
        foldl (\acc i -> acc + picker k i) 0 [0 .. k `div` 2]

main :: IO ()
main = do
    [_, k] <- map read . words <$> getLine :: IO [Int]
    xs <- map read . words <$> getLine :: IO [Int]
    print $ solve k xs
nolineage.comAkkoma

My first “up close & personal” with monad stacks. Rationalized and successfully changed a few

IO Either ErrorString a

(no, I don’t remember how I got there)

to

ExceptT ErrorString IO a

so I can happy-path chain some “might fail and I want to report why nicely” functions.

After a day or so of struggles, it’s starting to make sense. (Hell, it took a bit just to find ExceptT did the work of Either.)

Wishing the struggle was lighter, but working through it.

nolineage.comAkkoma
Continued thread
Also hitting my first real "monad stack"-type concerns with this one as the db functions are naturally built over IO -- that's "fun." Similarly, parsing BSON Document returns from mongo with the Value type...

And there's that frequent irritation that keeps hitting with nesting case statements... Did a Document return? Is the (Label, Value) in there? Is the Value the expected type? The voice in the back of my head: "Of course it's there -- it's by spec! Of course it's the right type -- it was generated by a process that guarantees it! I could have had this done in minutes in #python!" Distracting mental cycles wondering if I can somehow isolate the chain of operations into a Maybe-esque block with the "good path" assumptions and fails elegantly handled...

Then the realizations: Yeah, look at all the assumptions... What would have happened if one was broken?

Sigh. It sucks to do things "right."

If nothing else, it makes the flags raise while considering the #python code :-p

#programing #haskell
nolineage.comAkkoma
Took a breather this past weekend with an objective to clear all the HackerRank python prep exercises -- well, mostly to clear some arbitrary points-level goal to earn a sixth "star." Bam! Points achieved, but no star? Oh -- only five stars for Python... LOL. Good refresher anyway, and good to see your (qualitative) level when you see your solution alongside others.

Anyway, back to "Problem Solving" exercises -- typically my #haskell vs the world's #python :-p

#programing
nolineage.comAkkoma

The evening #python distraction from HackerRank: Break a string into chunks of length k and print the unique characters in each chunk (preserving order).

def solve(string, k):
    batches = batched(string, k)
    m=map(lambda b: str.join("", unique_everseen(b)), batches)
    print(*m, sep="\n")

batched is from itertools; unique_everseen is from more-itertools.

Looking at everyone’s solutions in the discussions section, you really have to wonder how anything works 💀

As for me, a little intellectual masturbation with an elegant, generator-based solution.

#functional #programing.

nolineage.comAkkoma

Style question for the object-oriented folks: If you have a “helper function” of sorts – something that basically improves readability of some other method but does nothing in particular to operate on an object’s state and likely has no reuse value outside the class, where do you put it?

  1. Keep it as a method?
  2. Make it a class / static method?
  3. Move it outside the class definition but maybe keep it inside the encapsulating module / namespace so it’s privately accessible by instances of the class?
  4. Get rid of it altogether and just inline it?
  5. ?

I’m leaning more toward “It doesn’t matter; it’s semantic gobbledygook and style preference,” but I haven’t been keeping up with the OO Jonses 🤷

nolineage.comAkkoma

What’s your opinion on using macros for stuff like that? I didnt like the nested match thingies and it could get quite huge with the auto-formatting, so I have made macros to unpack results, options and results of an option since I use these ALOT xD Now Im wondering... Is this an okay thing to do or too confusing for others? I made sure to name the macros properly and I guess you can see from the code what they do. #RustLang #coding #programing #tech #dev #development

#programing #proTip

Resist the urge to make temporary files with names like foo, foo2, foo3, foo.old, foo.old2, foo.new, newfoo2.old and so on.

Trust me. That which is new will become old. Your throw away scripts will become critical infrastructure.

Use meaningful names, even for temporary things, and have some sort of version control.

"There is nothing as temporary, as that which is called permanent, and nothing as permanent, as that which is called temporary."