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 » Mon Dec 15, 2014 11:08 am

^ That's brilliant Joe! i haven't thought of that for sure.

What if the entered string contained number(s)? Do you also map the number to prime numbers?
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 » Mon Dec 15, 2014 11:30 am

I hadn't considered that numbers would be permitted, but I guess that in this case, it might be necessary as (diff3 would be an anagram of 3fidf?) It would stand to reason that by mapping 0-9 to primes that those could be covered as well. My Schema currently is running a string-ref via iteration through the entire string (thus breaking it into chars) and then using a cond statement (case-switch in Scheme) to determine the value of it, which is then added into a globally declared value to keep up with the total value. Adding 10 more checks to it really wouldn't be a huge issue. Still, this is a pretty big project for someone who has to look up syntax all the time. :D

Perhaps the next few days worth of programming Challenge should be "Do MB's Challenge, covering as many cases as possible in an efficient manner?"

...let's make that official!
Image
(The girl in the back is how I've been feeling about this problem...it MUST be solved.)
|>>BBQ Roaster, Alpha Branch<< | >> clinky << | >> X11 must die << |
Thanks BASIC

machinebacon
Baconator
Posts: 10253
Joined: Thu Sep 16, 2010 11:03 am
Location: Pfälzerwald
Contact:

Re: Daily Programming Challenge:

Unread post by machinebacon » Mon Dec 15, 2014 4:07 pm

Code: Select all

#!/bin/bash
read yourstring
length=${#yourstring}
echo "Length is $length"
v=`echo $length*$length|bc`
echo "$v possible variations (with repetitions)"
for a in `seq $v`
do
        echo "$yourstring" | fold -w1 | sort -R | perl -ne 'chomp;print;'
        echo
done
No idea if the possible variations are indeed 'number of letters * number of letters'. If yes, yes. If no, I'm stupid :)

Edit: yeah I am stupid, they are repeating. Edited the shit above :)
..gnutella..

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

Re: Daily Programming Challenge:

Unread post by GekkoP » Mon Dec 15, 2014 4:19 pm

This challenge is really making me struggle. Another proof of my poor programming skills, of course. I'll keep working on it.

machinebacon
Baconator
Posts: 10253
Joined: Thu Sep 16, 2010 11:03 am
Location: Pfälzerwald
Contact:

Re: Daily Programming Challenge:

Unread post by machinebacon » Mon Dec 15, 2014 5:20 pm

http://math.stackexchange.com/questions ... variations

Just dropping this here for future reference.
..gnutella..

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 » Wed Dec 17, 2014 7:08 am

Okay, so some of us are struggling with this one. I'm now about 99% sure that my algorithm will work, and should be applicable in any language that allows breaking down strings into single chars. (Read: most any of them.) Despite MB's bash script, I want to see victory here for everyone's language of choice, so let's break this down into a series of smaller problems which we can later tie into a full-fledged solution.

Image
Today's Challege for the Stragglers (myself included).

Write a function that takes a string as input, parses the string and breaks it down into individual chars, regardless of the length of the string (up to a sane amount.) Have each section simply display the character for now, maybe with some additional notation (line return, dash, whatever) just so that we know that we're dealing with only one section at a time. This is only step 1 of this, so treat it like a huge project. Make it flexible, and notate what it does in whatever commenting or name-scheme makes sense to you.
|>>BBQ Roaster, Alpha Branch<< | >> clinky << | >> X11 must die << |
Thanks BASIC

machinebacon
Baconator
Posts: 10253
Joined: Thu Sep 16, 2010 11:03 am
Location: Pfälzerwald
Contact:

Re: Daily Programming Challenge:

Unread post by machinebacon » Wed Dec 17, 2014 7:11 am

echo $1 | fold -w1

(yeah I know, I'm an asshole) ;)
..gnutella..

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 » Wed Dec 17, 2014 8:40 am

^Nah, you're keeping things lighthearted. This is a pain to do from a lower-level, we need a bit a bash levity.

Code: Select all

;; Display our char, and a note that they're split.
(define (charpull s i)
  (display (string-ref s i))
  (print "->")
  (s_iter s (add1 i)))

;; Tight iterative sequencer based on length
(define (s_iter s i)
  (if (>= i (string-length s))
      (exit0)
      (charpull s i)))

;; I like using 'exit0' to call exits. :D
(define (exit0) (print "done\n"))
Looks like this, will use this in the next step, and I plan to integrate the '0' starting numeral when bringing in ARGVs later:

Code: Select all

(s_iter "test" 0)
t->
e->
s->
t->
done
|>>BBQ Roaster, Alpha Branch<< | >> clinky << | >> X11 must die << |
Thanks BASIC

User avatar
franksinistra
Ivana Fukalot
Posts: 1093
Joined: Mon Jan 27, 2014 2:03 am
Location: 印尼国

Re: Daily Programming Challenge:

Unread post by franksinistra » Wed Dec 17, 2014 9:38 am

This script is applicable to Joe's challenge

Code: Select all

(defn string-to-chars [s]
  (for [i (range (count s))]
    (get (s i))))
good thing is by applying 'int' function to the list of chars , i'll get a list of numbers
(MB's challenge, using Joe's suggested method)

Code: Select all

(defn string-to-number [s]
  (for [i (range (count s))]
    (int (get s i))))
and count the value of the numbers generated from the string

Code: Select all

(defn string-value [s]
  (reduce + s))
next thing i do is using dmenu_run cache, converting each program name to numbers
note: the code sucks big time

Code: Select all

(def prog-list (re-seq #"\w+" (slurp "dmenu_run")))

(defn list-of-programs [x]
  (for [i (range (count prog-list))]
    (string-value
      (string-number
        (nth (seq prog-list) (- (inc i) 1))))))

(list-of-programs prog-list)
then .... I'm stuck (as i don't intend to use java interop, or extra clojure library and i got too lazy to think)
rice no more.

machinebacon
Baconator
Posts: 10253
Joined: Thu Sep 16, 2010 11:03 am
Location: Pfälzerwald
Contact:

Re: Daily Programming Challenge:

Unread post by machinebacon » Wed Dec 17, 2014 2:48 pm

^ the dmenu_run cache idea is great, though: it doesn't look for documents or text files, only for executables that were scanned by 'menu'. :)
..gnutella..

User avatar
franksinistra
Ivana Fukalot
Posts: 1093
Joined: Mon Jan 27, 2014 2:03 am
Location: 印尼国

Re: Daily Programming Challenge:

Unread post by franksinistra » Wed Dec 17, 2014 3:41 pm

^ whoops i guess i misunderstood your instruction regarding the challenge

hahaha stupid me
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 » Thu Dec 18, 2014 10:15 am

franksinistra wrote: good thing is by applying 'int' function to the list of chars , i'll get a list of numbers
(MB's challenge, using Joe's suggested method)
Not 100% sure how this plays out, but if they aren't primes (such as the ascii values which are normally obtained from converting char->int), then there is a possibility of having non-anagrams with the same value and length. Primes only work because of their non-divisive properties. Your example may work fine, and this could be a total overkill from my part, but I did want to at least mention it.

Anyhow, more checks (for me at least)
Image

Write a function that parses individual characters, assigning a prime number value specific to the character...or convince me that it's not necessary (I am actually curious if I'm overkilling here Frank, but I don't think I am.)
|>>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 » Thu Dec 18, 2014 11:32 am

Okay, this is a big block...I still think that this will prove to be significantly faster than recursion in the long run. I'm currently using verbose printing to remind myself what I want to do with these numbers at the next step. This works by checking values for chars, upper or lowercase Roman alphabet being viewed as equal.

Code: Select all

;; Placeholder parser
(define (parser i)
  (cond
   ((or (= i 97) (= i 65)) (print "it's an a, add 2"))
   ((or (= i 98) (= i 66)) (print "it's a b, add 3"))
   ((or (= i 99) (= i 67)) (print "it's a c, add 5"))
   ((or (= i 100) (= i 68)) (print "it's a d, add 7"))
   ((or (= i 101) (= i 69)) (print "it's an e, add 11"))
   ((or (= i 102) (= i 70)) (print "it's an f, add 13"))
   ((or (= i 103) (= i 71)) (print "it's a g, add 17"))
   ((or (= i 104) (= i 72)) (print "it's an h, add 19"))
   ((or (= i 105) (= i 73)) (print "it's an i, add 23"))
   ((or (= i 106) (= i 74)) (print "it's a j, add 29"))
   ((or (= i 107) (= i 75)) (print "it's a k, add 31"))
   ((or (= i 108) (= i 76)) (print "it's an l, add 37"))
   ((or (= i 109) (= i 77)) (print "it's an m, add 41"))
   ((or (= i 110) (= i 78)) (print "it's an n, add 43"))
   ((or (= i 111) (= i 79)) (print "it's an o, add 47"))
   ((or (= i 112) (= i 80)) (print "it's a p, add 53"))
   ((or (= i 113) (= i 81)) (print "it's a q, add 59"))
   ((or (= i 114) (= i 82)) (print "it's a r, add 61"))
   ((or (= i 115) (= i 83)) (print "it's an s, add 67"))
   ((or (= i 116) (= i 84)) (print "it's a t, add 71"))
   ((or (= i 117) (= i 85)) (print "it's a u, add 73"))
   ((or (= i 118) (= i 86)) (print "it's a v, add 79"))
   ((or (= i 119) (= i 87)) (print "it's a w, add 83"))
   ((or (= i 120) (= i 88)) (print "it's an x, add 89"))
   ((or (= i 121) (= i 89)) (print "it's a y, add 97"))
   ((or (= i 122) (= i 90)) (print "it's a z, add 101"))
   ((= i 48) (print "it's a zero, add 103"))
   ((= i 49) (print "it's a one, add 107"))
   ((= i 50) (print "it's a two, add 109"))
   ((= i 51) (print "it's a three, add 113"))
   ((= i 52) (print "it's a four, add 127"))
   ((= i 53) (print "it's a five, add 131"))
   ((= i 54) (print "it's a six, add 137"))
   ((= i 55) (print "it's a seven, add 139"))
   ((= i 56) (print "it's an eight, add 149"))
   ((= i 57) (print "it's a nine, add 151"))))
...and to update yesterday's code to make sense of it:

Code: Select all

(define (charpull s i)
  (parser (char->integer (string-ref s i))) ;; This step to our parser
  (print "->")
  (s_iter s (add1 i)))
example run:

Code: Select all

#;2> (s_iter "test1" 0)
it's a t, add 71
->
it's an e, add 11
->
it's an s, add 67
->
it's a t, add 71
->
it's a one, add 107
->
done
|>>BBQ Roaster, Alpha Branch<< | >> clinky << | >> X11 must die << |
Thanks BASIC

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

Re: Daily Programming Challenge:

Unread post by GekkoP » Thu Dec 18, 2014 11:36 am

This is getting way beyond my programming skills. I'll follow so I can learn something.

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 » Thu Dec 18, 2014 12:29 pm

For sure GP, this may also prove to be above mine (in Scheme, at least) as well, but it's really pretty simple to understand if you realize the 'why' I'm going this direction. I'm writing for the machine, not for the programmer's sake a bit. There is an assumption I'm making:

Assumption= Compounding a set of prime numbers and ensuring that the set is the same length will provide a single number that is unique to the list, regardless of its order.

Beyond that, I'm now using iteration to break a string into individual characters, and reminding myself which prime I wish to assign to it later. I want (== A a) to be true, so I include the (char->integer) values for either in my condition switch. I'm using a cond, because as soon as we have a true value, we can ignore further tests. Should prove faster in the long run. Check this out, though....see if I'm making sense.

Code: Select all

#;2> (s_iter "TeSt" 0)
it's a t, add 71
->
it's an e, add 11
->
it's an s, add 67
->
it's a t, add 71
->
done

#;3> (s_iter "estt" 0)
it's an e, add 11
->
it's an s, add 67
->
it's a t, add 71
->
it's a t, add 71
->
done
To understand where this is heading, it might help to read http://www.mathsisfun.com/numbers/funda ... metic.html, especially the "there is only one way to do that in any case"...and just keep in mind that both addition and multiplication are transitive. I am almost certain that this will work in all cases, and is an interesting way to enumerate un-ordered lists. That's what I'm shooting for, not just for anagrams, but a method that works for any list that can be mixed up.
|>>BBQ Roaster, Alpha Branch<< | >> clinky << | >> X11 must die << |
Thanks BASIC

pidsley
Hermit
Posts: 2539
Joined: Wed Oct 17, 2012 12:31 pm

Re: Daily Programming Challenge:

Unread post by pidsley » Thu Dec 18, 2014 7:36 pm

Finally got a Haskell version of an old challenge working. I shamelessly stole Joe's logic and reimplemented it in Haskell. There are much better ways to do this (Haskell allows you to map a function to a list, and a string is just a list of chars, and don't even ask me about folding) but I'm not there yet.

Code: Select all

pc str n = do 
    print (str !! n)
    iter str (n + 1)

iter str n = do
   if (n >= length str) then
        putStrLn "done"
   else
        pc str n        

main = do
    let str = "test"
    iter str 0

Code: Select all

$ runhaskell str.hs
't'
'e'
's'
't'
done

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 » Thu Dec 18, 2014 11:15 pm

There's probably (read: almost certainly) better ways to do this in Scheme as well. I don't know those yet, though, so I don't use them. :)
|>>BBQ Roaster, Alpha Branch<< | >> clinky << | >> X11 must die << |
Thanks BASIC

User avatar
slartie
Grump
Posts: 416
Joined: Tue Jun 18, 2013 12:35 pm
Location: Denmark
Contact:

Re: Daily Programming Challenge:

Unread post by slartie » Thu Dec 18, 2014 11:23 pm

I'm not going to get sucked into this. Nope.

User avatar
Dr_Chroot
Alfalfa
Posts: 1100
Joined: Mon Jun 09, 2014 9:49 pm
Location: among the sagebrush
Contact:

Re: Daily Programming Challenge:

Unread post by Dr_Chroot » Fri Dec 19, 2014 12:04 am

^ Nice to see you back on here, slartie! These guys are amazing with what they can do, aren't they? I feel very fortunate to have found this community :) (I shudder to think that almost exactly one year ago I was proudly informing relatives that I was now running... Ubuntu.) I hope to see more newsletters :D
Fight internet censorship.
EFF | Tor Project | Bitcoin

"There have been times throughout American history where what is right is not the same as what is legal. Sometimes to do the right thing you have to break the law." - Edward Snowden

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 12:36 am

Good hearing from you, Slartie. You're 'actually' a Schemer, if I recall correctly. Could be interesting... :)
|>>BBQ Roaster, Alpha Branch<< | >> clinky << | >> X11 must die << |
Thanks BASIC

Post Reply