Daily Programming Challenge:

Programming (no configs, no support)
Forum rules
Only original work ;)
User avatar
franksinistra
Ivana Fukalot
Posts: 1093
Joined: Mon Jan 27, 2014 2:03 am
Location: 印尼国

Re: Daily Programming Challenge:

Unread post by franksinistra » Fri Dec 19, 2014 4:57 am

@DJ: I looked at your reasoning, and done a bit of research i guess my method would generate faulty and overly-complicated solution (like writing too much codes :D) in the end.

Anyway, i take no credit for the generate-prime function. It was a port (and it's an ugly one) of javascript code found here

Code: Select all

(defn generate-prime
  [] ;; a higher order function
  (letfn [(sieve [a b] ;; a is a map of numbers, b is a number  
            (loop [X a Y b]
              (if (nil? (get X Y))
                    (cons Y (lazy-seq
                             (sieve
                              ;; javascript array is a persistent version of a transient collection
                              ;; hence the use of 'persistent!' and 'transient' 
                              (persistent! (assoc! 
                                            (transient X)
                                            (* Y Y) (list Y))) (inc Y))))
                    (let [factors-of-Y (get X Y)
                          kv (mapcat
                               list
                               (map #(+ % Y) factors-of-Y)
                               (map #(cons % (get X (+ % Y) (list)))
                                    factors-of-Y))]
                      (recur (persistent!
                              (dissoc!
                               (apply assoc! (transient X) kv) Y))
                             (inc Y))))))]
    (sieve {} 2))) ;; a is initialized as empty map, b is set to two since the lowest prime is two
here's the string to prime numbers function

Code: Select all

(defn string->primes [s]
  (for [i (range (count s))]
    (let [print-prime #(printf "found %s, add %d \n" (get s i) (nth (generate-prime) %))
          charify-s (get s i)]
      (cond
        (or (= charify-s \a) (= charify-s \A)) (print-prime 0)
        (or (= charify-s \b) (= charify-s \B)) (print-prime 1)
        (or (= charify-s \c) (= charify-s \C)) (print-prime 2)
        (or (= charify-s \d) (= charify-s \D)) (print-prime 3)
        (or (= charify-s \e) (= charify-s \E)) (print-prime 4)
        (or (= charify-s \f) (= charify-s \F)) (print-prime 5)
        (or (= charify-s \g) (= charify-s \G)) (print-prime 6)
        (or (= charify-s \h) (= charify-s \H)) (print-prime 7)
        (or (= charify-s \i) (= charify-s \I)) (print-prime 8)
        .. ;; keep going until char \9))))
the result will be

Code: Select all

(string->prime ("aBHbA")
;=> 
found a, add 2
found B, add 3
found H, add 19
found b, add 3
found A, add 2
Thanks Joe for the suggestion, and for reminding me that i was wrong :D


@Gekko: This too was beyond my programming skill, it took quite a lot of trials and errors (meaning hours) and C-c C-d g on the CIDER repl like everytime.

@Pidsley: though i don't quite understand Haskell, your code looks beautiful
rice no more.

User avatar
DebianJoe
Frame Buffer
Posts: 1915
Joined: Mon Jul 01, 2013 5:41 am
Location: emacs.d

Re: Daily Programming Challenge:

Unread post by DebianJoe » Fri Dec 19, 2014 9:37 am

franksinistra wrote:@Pidsley: though i don't quite understand Haskell, your code looks beautiful
^ I would say this is the general consensus on Pidsley's Haskell. "We don't know how it works, but it looks beautiful."

Also, I wasn't totally sure that your method wouldn't work. Hell, I'm not TOTALLY sure that mine will. That's what makes this fun. :D

Image
++++++++++++++
...in trying to optimize our checks for speed, we need to look at the WHAT we are going to be comparing. We will have 2 values, one that comes from a string that we input. That value doesn't need to change or be figured more than once for the entire run of our program. We should probably come up with a method to figure its value, and then push it off into its own little area in memory. Later, we can use our previous methods of adding primes to mutate this, but for now...a proof of concept should work fine.

Today's challenge: Create a function or set of functions that initialize a variable, and that can perform repeated operations to mutate the variable. It needs to be possible to be called outside of the function by name. (global variable mutation test, could be interesting in some languages)
|>>BBQ Roaster, Alpha Branch<< | >> clinky << | >> X11 must die << |
Thanks BASIC

User avatar
DebianJoe
Frame Buffer
Posts: 1915
Joined: Mon Jul 01, 2013 5:41 am
Location: emacs.d

Re: Daily Programming Challenge:

Unread post by DebianJoe » Fri Dec 19, 2014 9:55 am

Here's what I've got, which seems to work as expected:

EDIT! - Since I was using multiplication, I had an init function which was meant to cover not simply multiplying by 0 which wouldn't actually ever change a value. Decided I could remove a whole step by not being stupid, simply declare ana_value as 1, and thus actually 'init' at step 1 with a call to muta_AV...which will mean less step checking later on.
Better Code:

Code: Select all

;; Declare our value to bind it
(define ana_value 1)
;; Create a call procedure
(define (get_AV) ana_value)
;; Provide a method to mutate it
(define (muta_AV x)
  (set! ana_value (* x ana_value)))
...example run. The load is my 'big-file' with all of this project so far stored in it. I wanted to be sure that my declare would work on load.

Code: Select all

#;1> (load-relative "/home/joe/lisp_scripts/schemes/anagram.scm")
; loading /home/joe/lisp_scripts/schemes/anagram.scm ...
#;2> (get_AV)
1
#;3> (muta_AV 2)
#;4> (get_AV)
2
#;5> (muta_AV 3)
#;6> (get_AV)
6
|>>BBQ Roaster, Alpha Branch<< | >> clinky << | >> X11 must die << |
Thanks BASIC

User avatar
GekkoP
Emacs Sancho Panza
Posts: 5877
Joined: Tue Sep 03, 2013 7:05 am

Re: Daily Programming Challenge:

Unread post by GekkoP » Tue Dec 30, 2014 5:30 pm

Since I am not good with the anagrams exercise, I offer you this one:

Code: Select all

(apply str (concat "Happy " (str (+ 1024 512 256 128 64 16 8 4 2 1))))

Post Reply