Ruby Challenge

Here is a ruby challenge for all you computer science lovers out there,
lets see how many ways we can solve this problem:

create a word frequency generator; that is, a program that takes in a
text file and generates a list of words and counts (you know, pairs of
things). Lets say you have a text file.

If the text file contained this sentence, the output of your program
would look something like this:

1 contained
1 file
1 if
1 like
1 look
1 of
1 output
1 program
1 sentence
1 something
1 text
1 would
1 your
2 the
2 this

Consider a “word” to be any space-delimited token. How would you also do
this with punctuation included?

Why does this sound like a school assignment?

file = 'sample.txt'
freq = Hash.new{|h,k| h[k] = 0}
File.read(file).scan(/\w+/) do |w|
  freq[w] += 1
end
freq.each do |w,c|
  puts "#{c} #{w}"
end

Untested.

A solution like this is in the Pickaxe book as well.

On Nov 5, 2011, at 5:59 AM, teresa nuagen wrote:

end

Untested.

There should be an “if” / “else” statement in a looping method, to add
the word in list or word count.

Careful, you don’t want to annoy the people who help you. The code
solves your problem admirably: paste this (don’t copy the ### marks)
into a file and run it, and you will get exactly the output you wanted:

freq = Hash.new{|h,k| h[k] = 0}
DATA.read.scan(/\w+/) do |w|
freq[w.downcase] += 1
end
freq.each.sort_by {|w,c| [c,w] }.each do |w,c|
puts “#{c} #{w}”
end

END

If the text file contained this sentence, the output of your program
would look something like this

Before claiming a proposed solution to a ‘challenge’ you posted is
missing something, have the decency at least to run it to see if it
works. In this case, Intransition’s solution sets the counter for each
word to zero (this is what the first line does: if you add a word to the
freq hash for the first time the counter is set to zero). The scan
method is your ‘looping method’ and executes the block at each
iteration. Finally, freq[w.downase] += 1 increases the counter for
each word. By defining, the zero default, Intransition’s solution
doesn’t require your if/else statement. So, please, next time, instead
of simply stating ‘there should be an if/else statement there’, how
about ‘cool solution! Can you explain to me why this works without an
explicit if/else statement?’.

On Sat, Nov 5, 2011 at 3:40 AM, teresa nuagen [email protected] wrote:

1 contained
1 would
1 your
2 the
2 this

Consider a “word” to be any space-delimited token. How would you also do
this with punctuation included?

Now, where’s the challenge? I rather get the impression that you want
to make other people do your work by spicing the task with the word
“challenge”. If that should indeed be the case please be aware that
this kind of behavior is not appreciated in our community.

Cheers

robert

Thomas S. wrote in post #1030247:

file = 'sample.txt'
freq = Hash.new{|h,k| h[k] = 0}
File.read(file).scan(/\w+/) do |w|
  freq[w] += 1
end
freq.each do |w,c|
  puts "#{c} #{w}"
end

There should be an “if” / “else” statement in a looping method, to add
the word in the list if it’s not there and a word count.

Robert K. wrote in post #1030275:

On Sat, Nov 5, 2011 at 3:40 AM, teresa nuagen [email protected] wrote:

1 contained
1 would
1 your
2 the
2 this

Consider a “word” to be any space-delimited token. How would you also do
this with punctuation included?

Now, where’s the challenge? I rather get the impression that you want
to make other people do your work by spicing the task with the word
“challenge”. If that should indeed be the case please be aware that
this kind of behavior is not appreciated in our community.

Cheers

robert

Don’t worry this is just for fun, because it’s a social ruby forum for
talented programmers and those who enjoy challenges. The key challenge
to this is to incorporate punctuations to it, writing the program with
punctuations in the words.

Totally unrelated to any husker computer science programs right? Like
the previous homework you posted about reversing a poem?

Uyen I’m not sure you understand how very public this forum is, and how
easily someone from your school could question your academic integrity
with information they find here.

Asking for advice is entirely welcome here; handing off homework is not.

Jonan S. wrote in post #1030330:

Uyen I’m not sure you understand how very public this forum is, and how
easily someone from your school could question your academic integrity
with information they find here.

You mean like the professor for the course? Because that would be me .
. . That “challenge” is copied almost verbatim from a problem set.

I would just like to take this opportunity to thank the members of this
board for their general vigilance in this matter. As we all know,
students (some students) try this kind of thing all the time, and yet
the community can be relied upon to call them on it.

No one asks you to do that, and certainly, no professor would expect
people who are otherwise just trying to be helpful to act as the ethics
police (nor could I blame anyone who answers the question in good
faith). But the truth is, someone nearly always calls them on it, and I
think those of us who teach programming are very grateful for your help.
You’re upholding the ethical standards of our discipline, and you really
do a great service to the profession by doing so.

Steve

You mean like the professor for the course? Because that would be me .
. . That “challenge” is copied almost verbatim from a problem set.

I would just like to take this opportunity to thank the members of this
board for their general vigilance in this matter. As we all know,
students (some students) try this kind of thing all the time, and yet
the community can be relied upon to call them on it.

No one asks you to do that, and certainly, no professor would expect
people who are otherwise just trying to be helpful to act as the ethics
police (nor could I blame anyone who answers the question in good
faith). But the truth is, someone nearly always calls them on it, and I
think those of us who teach programming are very grateful for your help.
You’re upholding the ethical standards of our discipline, and you really
do a great service to the profession by doing so.

Steve

I’m sorry for the trouble. I’m a friend who she came to for
help with this problem and she did not ask me to post this. I created
this forum acct under her email address which i know now is a huge
mistake for jeopardizing her “honesty” identity like this. She asked me
for help and I didn’t know how to do it, so instead I took the easy
route and ask it in here forgetting that it is public. She told me that
what i was doing was wrong and that if i did not know then i shouldn’t
ask the public to try and solve it. She said “i rather do this on my own
then to turn in somebody else’s work. In my school this is considered
academic dishonesty, and as a senior soon to graduate, I cannot risk it.
If I do not understand it, then I will not turn it in. I’m better off
dropping the course then to pass on an assignment that i did not write.”
Those were her exact words and once she officially dropped the class on
Saturday, I knew that she was serious and refuse to get involve in what
is posted on here. I may have asked the community for help on a problem,
but she did a noble choice in refusing to use the problem and would
rather not turn the assignment. she said that she has missed 2
assignment and because of that, her only resort is to drop the class.
She said she is very devastated by this because you’re her favorite
teacher, but this is the only option since she didn’t do the problems.

I’m sorry for the trouble this has caused you.

On Wed, Nov 9, 2011 at 4:52 AM, aseret nuagen [email protected] wrote:

police (nor could I blame anyone who answers the question in good
faith). But the truth is, someone nearly always calls them on it, and I
think those of us who teach programming are very grateful for your help.
You’re upholding the ethical standards of our discipline, and you really
do a great service to the profession by doing so.

You are welcome!

If I do not understand it, then I will not turn it in. I’m better off
I’m sorry for the trouble this has caused you.
You are focusing on the “get free help” matter but if I understand
this correctly and all this is true then this can be seen as a case of
identity theft, which may even be subject to legal prosecution. I
have to say I find impersonating someone else by (ab)using their email
address even worse than trying to get something for free from the
community. Why on earth did you not use your own email account for
this? This is such a great community and I am very sad seeing things
like this happening here.

Regards

robert

Come on. Let’s not be so harsh. If truth is being told, then this girl
has
now dropped a class over it. And what good does that serve? Now she
won’t
be learning any Ruby, Programming, or (IMHO) how to truly “Think”.
Shaming
her or her friend isn’t going to make the world a better place.
Encouraging
her to earnestly try, on her own, regardless of the outcome or the grade
she might receive is more noble. As long as she learns, even just a
small
portion of what her classmates manage, then at least it is something
positive. And relative growth is far more important than absolutes. For
what might seem small to you and me can in fact be quite huge to a
newbie.

Hmm… That last post from Uyen sounds very much made up to me…

But I agree with Intransition. Well said :slight_smile:

I’ve only been on this list for a few weeks and already I’m feeling as
if I’ll be frequenting for a long time.

i attached a photo of the message she sent me. She wanted the truth be
told and with my apologies that’s what i did. I’m sorry for turning the
problem into a “challenge” i’m international not from this different
culture so i don’t know.

I’m also lamenting the loss of another learner.

Good luck Uyen, I hope you keep programming.

On Nov 9, 2011, at 04:50 , Intransition wrote:

Come on. Let’s not be so harsh.

Leave it to you to side with intellectual dishonesty.

No, let’s be harsh. If this kid has gotten to college level and not
learned that taking credit for someone else’s work is not only wrong,
but going to bite you in the ass hard once you leave academia, then it
is certainly time to drive that lesson home. What we don’t need out
there is another plagerist.

If truth is being told, then this girl has now dropped a class over it.

You don’t know what the policy of the school is. Don’t assume you have
any idea what the outcome will be.

And what good does that serve?

It serves a lot of good. Both for her and for society in general. Look
at the devastating effects of the work of Victor Ninov and Jan Hendrik
Schn. A distinct lack of ethics and intellectual dishonesty caused this.
Do we need more of it? No.

Now she won’t be learning any Ruby, Programming, or (IMHO) how to truly “Think”.

Again, you don’t know that. You have no idea what the disciplinary
policy of this school is.

Shaming her or her friend isn’t going to make the world a better place.
Encouraging her to earnestly try, on her own, regardless of the outcome or the
grade she might receive is more noble. As long as she learns, even just a small
portion of what her classmates manage, then at least it is something positive. And
relative growth is far more important than absolutes.

So, taking credit for someone else’s work, as long as she can claim she
learned something from it, is a good thing in your opinion. What happens
when she’s out in the real world with a real job and she tries that? She
gets fired, without benefits, and that reference is forever burned. It
probably follows her around as well.

For what might seem small to you and me can in fact be quite huge to a newbie.

Which is why driving this lesson home now is a Good Thing.

On Nov 9, 2011, at 14:23 , Adam P. wrote:

“Think”.

Again, you don’t know that. You have no idea what the disciplinary policy
of this school is.

I know you seem to like to get to the point in this aggressive way, and I
have no idea what existing issues you have with Intransition are (and I
don’t want to know), but the comments are being made based on the prior
post that the girl has already dropped out because she finds the class
difficult, not because of whatever assumptions about disciplinary policies.

This has less to do with trans and more to do with my absolute
intolerance for intellectual dishonesty. I’ve been that teacher before.
My best friend actually worked on element 118 with Victor Ninov. (and
Trans has reused my code and violated my license terms). I have a zero
tolerance policy for such behavior and think we should, as a society,
discipline/blacklist/shun (as appropriate) said people.

That said, I did overlook that post and misunderstand the alleged
explanation as it was given (simple TL;DR error on my part). Thank you
for calling me to task. The rest of my point still stands.

More predictable twists than an M. Night Shyamalan movie!

Posted by Sam R.
Hmm… That last post from Uyen sounds very much made up to me…

Even if I try to fight my more cynical instincts, it still sounds
like it’s the same person.

Which is fine, we all make mistakes. I just hope whoever it is that did
whatever they did (or did not do) will keep learning Ruby. I also hope
that whoever that was that was posting will work on their persuasive
techniques.

-Luke

The Lord have mercy on your soul, Ryan. You must honestly be one of the
saddest persons in the world. You had a fit over six lines of code that
I
used from MiniTest to get Turn to work with MiniTest --b/c you changed
the
MiniTest API and I had to do a work around. And once you pointed out
this
bit of code, your copyright notice was added to the project --a project
I
worked extensively on and never asked for one iota of credit.

No one else recalls it now, I’m sure, but the Hoe project was a complete
ripoff of my original Reap project. But I didn’t complain. I saw that
you
did a good job on it and I admittedly muffed up the management of the
original project (I was completely new to project management at the
time).
Is that what all this hate is about? You stole my project and have been
compensating for the guilt this whole time?

On Wed, Nov 9, 2011 at 22:08, Ryan D. [email protected]
wrote:

Again, you don’t know that. You have no idea what the disciplinary policy
of this school is.

I know you seem to like to get to the point in this aggressive way, and
I
have no idea what existing issues you have with Intransition are (and I
don’t want to know), but the comments are being made based on the prior
post that the girl has already dropped out because she finds the class
difficult, not because of whatever assumptions about disciplinary
policies.