Is this a bug on RubyMonk or something flaw in my logic thinking? - logic

I am learning Ruby with Rubymonk.com
Below is the project description:
Hiring Programmers - Boolean Expressions in Ruby
Let us say you are trying to recruit team-members for your new startup! Given a candidate, you need an expression that will tell you whether they fit into certain types. This is how a candidate object would look:
candidate.years_of_experience = 4
candidate.github_points = 293
candidate.languages_worked_with = ['C', 'Ruby', 'Python', 'Clojure']
candidate.applied_recently? = false
candidate.age = 26
We are looking to hire experienced Ruby programmers. Our ideal candidate has 2 or more years of experience, but some programmers become really good even before that. We'll consider their Github points (a nice indicator of a good programmer), and even if they are not experienced, candidates with 500 Github points or more can apply. And there is one more catch: Ruby being a cool and awesome language, a lot of smart youngsters are very good at it. We love those kids, but for this particular job we'd rather have them study at school than work. Let us filter out candidates who are younger than 15. Also we don't want to consider candidates who applied recently for this opening.
Base on above description, I conclude that items listed below must be true:
candidate.languages_worked_with.include?('Ruby')
candidate.years_of_exprience >= 2 || candidate.github_points >= 500
candidate.age > 15
candidate.applied_recently? == false
And my answer is:
is_an_experienced_programmer = (candidate.years_of_exprience >= 2
|| candidate.github_points >= 500) && candidate.languages_worked_with.include? 'Ruby'
&& (candidate.age > 15) && !(candidate.applied_recently?)
but then answer is:
is_an_experienced_ruby_programmer = (candidate.years_of_experience >= 2
|| candidate.github_points >= 500) && (candidate.languages_worked_with.include? 'Ruby')
&& ! (candidate.age < 15 || candidate.applied_recently?)
The only difference between my answer and the answer is:
(candidate.age > 15) && !(candidate.applied_recently?)
above saying candidate must older than 15 and haven't applied recently.
and the answer:
! (candidate.age < 15 || candidate.applied_recently?)
above code basically saying, candidate can not younger than 15 and haven't applied recently.
Aren't they the same? Or something flawed in my logic?

Its almost same only thing you are missing is >=
(candidate.age >= 15) && !(candidate_applied_recently?)
and that's why you might be getting the wrong answer.
Hope it helps.

I conclude that there is something wrong in the Testing Logic with the project on RubyMonk:
When I use the code below, I passed the test:
is_an_experienced_ruby_programmer = (candidate.years_of_experience >= 2 || candidate.github_points >= 500) && (candidate.languages_worked_with.include? 'Ruby') && !candidate.applied_recently? && (candidate.age > 15)
And when I use below, I failed the test:
is_an_experienced_programmer = (candidate.years_of_exprience >= 2 || candidate.github_points >= 500) && (candidate.languages_worked_with.include? 'Ruby') && (candidate.age > 15) && !(candidate.applied_recently?)
So
!(candidate.applied_recently?) && (candidate.age > 15)
is different to
(candidate.age > 15) && !(candidate.applied_recently?)
in Rubymonk's mind?

Related

EF Core 5 Sum difference between two date times

We have upgraded to net 5 / ef core 5, and many of our queries have needed "fixing" due to the changes that have been made.
In reality, the queries were horribly inefficient, because the LINQ could not be translated to SQL and now this has been brought to our attention; one solution is to force this to be worked on the client instead of the SQL Server... however I would like this to make this more efficient.
Below is a query that I can't find a way to do efficiently:
DateTime toDate = DateTime.Now.AddDays(NumberofDays + 1).Date;
MyInsights.AvailableHours = DatabaseContext.WorkCenterSchedules
.Where(x => x.ForWorkCenter.WorkCenterId == WorkCenterID && x.dateTo > DateTime.Now && x.dateTo < toDate)
.Sum(y => (y.dateTo - (DateTime.Now > y.dateFrom ? DateTime.Now : y.dateFrom)).TotalMinutes) / 60;
The error is:
This could be changed to the following:
DateTime toDate = DateTime.Now.AddDays(NumberofDays + 1).Date;
List<WorkCenterSchedule> schedules = DatabaseContext.WorkCenterSchedules
.Where(x => x.ForWorkCenter.WorkCenterId == WorkCenterID && x.dateTo > DateTime.Now && x.dateTo < toDate).ToList();
MyInsights.AvailableHours = schedules.Sum(y => (y.dateTo - (DateTime.Now > y.dateFrom ? DateTime.Now : y.dateFrom)).TotalMinutes) / 60;
However, this is far from ideal. I know EF Core isn't ideal for complex queries, but I feel like I am missing something obvious here, is there something I can do to improve this?
You might be able to leverage SQL Server's DATEDIFF function:
EF.Functions.DateDiffMinute(y.dateTo, DateTime.Now > y.dateFrom ? DateTime.Now : y.dateFrom)

'<=' and '>=' operators do not work

To check if the the installer is installed, I did:
installer status |grep Version| cut -c12-13
The ouput [sic] says:
installer not found
But if it were installed, it would say 11 or 10 (any numeric). If the output is <=10 || >=11, then it would say not installed, and would proceed with the installation. In the library, it gives:
def get_installer_linux_version
begin
cmd = Mixlib::ShellOut.new('installer status |grep Version| cut -c12-13')
cmd.run_command
rescue Errno::ENOENT => e
return '0.0'
end
return 'Version 10' if cmd.stdout.include? '10'
return 'Version 11' if cmd.stdout.include? '11'
end
In the install recipe is:
if get_installer_linux_version.to_i <= 10 || get_installer_linux_version.to_i >= 11
log 'installer is installed'
else
log 'installer is not installed so procceding with the installation'
There are numerous things in your code that could be considered flawed, but for the sake of your question, I will focus on that.
get_installer_linux_version returns a few possible values. Either the string "version 10", "version 11", "0.0", or nil. You call this function, then call to to_i on it.
Maybe this will help illistrate it:
"version 10".to_i
#=> 0
I am going to guess that is not the intended behavior. No matter what happens in your get_installer_linux_version, the returned string will always be 0 after you call to_i on it. You then make condition that <= 10, which 0 is still less than 10, and it logs "installer is installed".
I would also venture to guess that using include? 'XX' is going to end up causing you problems when dealing with version numbers, but that is another question.
To further illustrate, let me write your code as it will always, always, always be as you have it written, and see if something looks off:
if 0 <= 10 || 0 >= 11
log 'installer is installed'
else
log 'installer is not installed so procceding with the installation'
What do you expect the result to be?? Although 0 is not ever going to be greater than 11, it will always be less than 10, so your result is always the same.
Just going to put this here, as it may be relevant the more I look at your sample.
>=
Checks if the value of left operand is greater than or equal to the
value of right operand, if yes then condition becomes true.
<=
Checks if the value of left operand is less than or equal to the value
of right operand, if yes then condition becomes true.

Pinescript duplicate alerts

I have created a very basic script in pinescript.
study(title='Renko Strat w/ Alerts', shorttitle='S_EURUSD_5_[MakisMooz]', overlay=true)
rc = close
buy_entry = rc[0] > rc[2]
sell_entry = rc[0] < rc[2]
alertcondition(buy_entry, title='BUY')
alertcondition(sell_entry, title='SELL')
plot(buy_entry/10)
The problem is that I get a lot of duplicate alerts. I want to edit this script so that I only get a 'Buy' alert when the previous alert was a 'Sell' alert and visa versa. It seems like such a simple problem, but I have a hard time finding good sources to learn pinescript. So, any help would be appreciated. :)
One way to solve duplicate alters within the candle is by using "Once Per Bar Close" alert. But for alternative alerts (Buy - Sell) you have to code it with different logic.
I Suggest to use Version 3 (version shown above the study line) than version 1 and 2 and you can accomplish the result by using this logic:
buy_entry = 0.0
sell_entry = 0.0
buy_entry := rc[0] > rc[2] and sell_entry[1] == 0? 2.0 : sell_entry[1] > 0 ? 0.0 : buy_entry[1]
sell_entry := rc[0] < rc[2] and buy_entry[1] == 0 ? 2.0 : buy_entry[1] > 0 ? 0.0 : sell_entry[1]
alertcondition(crossover(buy_entry ,1) , title='BUY' )
alertcondition(crossover(sell_entry ,1), title='SELL')
You'll have to do it this way
if("Your buy condition here")
strategy.entry("Buy Alert",true,1)
if("Your sell condition here")
strategy.entry("Sell Alert",false,1)
This is a very basic form of it but it works.
You were getting duplicate alerts because the conditions were fulfulling more often. But with strategy.entry(), this won't happen
When the sell is triggered, as per paper trading, the quantity sold will be double (one to cut the long position and one to create a short position)
PS :You will have to add code to create alerts and enter this not in study() but strategy()
The simplest solution to this problem is to use the built-in crossover and crossunder functions.
They consider the entire series of in-this-case close values, only returning true the moment they cross rather than every single time a close is lower than the close two candles ago.
//#version=5
indicator(title='Renko Strat w/ Alerts', shorttitle='S_EURUSD_5_[MakisMooz]', overlay=true)
c = close
bool buy_entry = false
bool sell_entry = false
if ta.crossover(c[1], c[3])
buy_entry := true
alert('BUY')
if ta.crossunder(c[1], c[3])
sell_entry := true
alert('SELL')
plotchar(buy_entry, title='BUY', char='B', location=location.belowbar, color=color.green, offset=-1)
plotchar(sell_entry, title='SELL', char='S', location=location.abovebar, color=color.red, offset=-1)
It's important to note why I have changed to the indices to 1 and 3 with an offset of -1 in the plotchar function. This will give the exact same signals as 0 and 2 with no offset.
The difference is that you will only see the character print on the chart when the candle actually closes rather than watch it flicker on and off the chart as the close price of the incomplete candle moves.

For-loop in lua with löve2D, deleting variable

i'm a bit beginner about coding, and my english isn't great, i hope i'll be able to make my question clear:
So I have a for-loop in my code, and 2 if in it, in each if, I see if something is true or false, if it's true, I delete a portion of the loop. like that:
for n=#Test,1, -1 do
if Test[n].delete == true then
table.remove(Test, n )
end
if Test[n].y > 0 then
table.remove(Test, n )
end
end
kind of like that, and my problem is, if the first if make Test[n] being deleted, then the game crash at the next if because Test[n] Doesn't exist anymore. I solved the problem by making 2 for-loop, one for each if.
But I saw someone who did it without 2 for-loop, and it's not crashing, I tried to figure what was wrong but I can't find it.
If someone could find what is wrong with what I wrote, I would be thankful!
So here is the moment that makes problem in my code, on line 9, if the condition are met, i table.remove(ListeTirs, n ), then on line 17, when code try to find it again for test it, it bug :
for n=#ListeTirs,1, -1 do
if ListeTirs[n].Type == "Gentils"
then local nAliens
for nAliens=#ListeAliens,1,-1 do
if
Collide(ListeTirs[n], ListeAliens[nAliens]) == true
then
ListeTirs[n].Supprime = true
table.remove(ListeTirs, n )
ListeAliens[nAliens].Supprime = true
table.remove(ListeAliens, nAliens)
end
end
end
if
ListeTirs[n].y < 0 - ListeTirs[n].HauteurMoitie or ListeTirs[n].y > Hauteur + ListeTirs[n].HauteurMoitie or ListeTirs[n].x > Largeur + ListeTirs[n].LargeurMoitie or ListeTirs[n].x < 0 - ListeTirs[n].LargeurMoitie
then
ListeTirs[n].Supprime = true
table.remove(ListeTirs, n)
end
end
I hope it's clear, I could post the whole code but I don't feel it's necessary, if it is, I will add it.
Thank you :)
for n=#Test,1, -1 do
local should_be_removed
-- just mark for deletion
if Test[n].delete = true then
should_be_removed = true
end
-- just mark for deletion
if Test[n].y > 0 then
should_be_removed = true
end
-- actually delete (at the very end of the loop body)
if should_be_removed then
table.remove(Test, n )
end
end

Ruby: divisible by 4

This works fine, but I want to make it prettier - and accommodate all values that are divisible by 4:
if i==4 || i==8 || i==12 || i==16 || i==20 || i==24 || i==28 || i==32
# ...
end
Any clever, short method to do this?
Try this:
if i % 4 == 0
This is called the "modulo operator".
There's also modulo, which allows you to do
420.modulo(4).zero?
There's nothing stopping you doing that with %, but it looks weird:
420.%(4).zero?
This is always a good conversation starter:
if (i & 3).zero?

Resources