How can I improve this program to get 24 with 4 numbers?

24 points is a small game. You must use + - * / to get the result of 24 out of 4 numbers. I wrote a Perl script to solve this problem. But I feel that my code is too long and it looks like C.

Hope someone can give me some advice. Thank you very much!

Another language is also possible, for example Python, Scala, F #, C ++.

 my @test_arr = (10, 4, 7, 6); my @oprator_arr = ('+', '-', '*', '/'); rec_cala(\@test_arr); sub rec_cala { my ($arr_ref) = @_; my @input_arr = (); push @input_arr, @$arr_ref; if (scalar(@input_arr) <= 1) { $result = eval $input_arr[0]; if ($result > 23.9 && $result < 24.1) { print $input_arr[0]; print " = 24\n"; } } else { my @perm_arr = perm(\@input_arr); foreach (@perm_arr) { my @next_arr = @$_; my $op1 = pop @next_arr; my $op2 = pop @next_arr; foreach (@oprator_arr) { @op_expr_arr = @next_arr; push @op_expr_arr, "($op1 $_ $op2)"; rec_cala(\@op_expr_arr); } } } } sub perm { my ($arr_ref) = @_; my @arr = @$arr_ref; my @result = []; while (scalar(@arr)) { my $curr_element = pop @arr; my @next_step = (); foreach $curr_array (@result) { $curr_len = scalar(@$curr_array); for ($i = 0; $i <= $curr_len; $i++) { push @next_step, [ (@$curr_array[0 .. $i], $curr_element, @$curr_array[$i + 1 .. $curr_len]) ]; } } @result = @next_step; } return @result; } 
+4
source share
2 answers

OK, two points:

1) You said "improve."

While golf is “cool” for codes and, if properly studied and applied, helps a developer increase his knowledge and gnaw at the nuances and depths of a chosen language, the resulting golf code is not generally an improvement from software with development point of view.

Although good, elegant code often leads to a shorter program, the opposite is not necessarily (or usually) true. Just shortening the code most likely does not mean that the code is harder to read and complicate, and these two qualities are pretty much important for good software development.

2) Having said that, Perl provides many syntax tools and idioms that provide better code, reducing it as a side effect. I will try to point out some possible changes to your code that will make it more idiomatic, in my opinion, although not necessarily shorter.


OLD:

 my @input_arr = (); push @input_arr, @$arr_ref; 

NEW:

 my @input_arr = (@$arr_ref); 

Explanation: You do not need to declare an array and initialize it separately.


OLD:

 if (scalar(@input_arr) <= 1) { 

NEW:

 if (@input_arr <= 1) { 

Explanation: Arrays in Perl, if evaluated in the scaalar context (which uses the numerical comparison operator, for example, <= ), evaluate the size of the array. So scalar() is redundant.


More to add later - you need to run

+4
source

You have a question as if you were looking for tips to improve a functioning program. But your program does not work under use strict and use warnings , and it does not work, as far as I can tell. Even the perm method perm violated, generating both warnings and incorrect results.

Before you can even make a simple program, you need to make sure that your building blocks are straight. One place to start is to figure out how to write the correct perm method, which will work under use strict and use warnings . If your goal is to solve this riddle yourself, that's great. Otherwise, check the Perl FAQ, How do I transfer N list items ?

+4
source

All Articles