Erlang Towers of Hanoi - functional-programming

Currently stuck on trying to implement the towers of hanoi using a collection. I am trying to follow the example in Java using stacks http://www.sanfoundry.com/java-program-implement-solve-tower-of-hanoi-using-stacks/, but I am getting
-module(toh).
-export([begin/0]).
begin() ->
Pegs = 3,
TowerA = createTowerA(N),
TowerB = [],
TowerC = [],
move(Pegs, TowerA, TowerB, TowerC).
%fills Tower A with integers from Pegs to 1.
createTowerA(0) -> [];
createTowerA(N) when N > 0 ->
[N] ++ createTowerA(N - 1).
%displays the towers
display(A, B, C) ->
io:format("~w\t~w\t~w~n", [A, B, C]).
move(Pegs, TowerA, TowerB, TowerC) ->
if Pegs > 0 ->
move(Pegs, TowerA, TowerC, TowerB),
Temp = lists:last(TowerA),
NewTowerC = C ++ Temp,
NewTowerA = lists:sublist(TowerA, length(TowerA) - 1),
display(NewTowerA, B, NewTowerC),
move(Pegs - 1, B, NewTowerA, NewTowerC);
end
When I try running the code, I get this error.
{"init terminating in do_boot",{undef,[{toh,begin,[],[]},{init,begin_i
t,1,[{file,"init.erl"},{line,1057}]},{init,begin_em,1,[{file,"init.erl"},{line,1
037}]}]}}
Crash dump was written to: erl_crash.dump
init terminating in do_boot ()
Can someone see why this is not working? I'm just trying to follow the sanfoundry example.

This code cannot compile, at least the variable C in move/4 is unbound when used. So it seems that you didn't compile this file before trying to execute it.
Although Erlang used a virtual machine, it must be compiled before execution.
There are other problems than the C variable in this code: you call move/4 recursively in the first line of the if, without using any returned value, this cannot have any effect. Also you are using a if statement with a bad syntax. the correct syntax is:
if
GuardSeq1 ->
Body1;
...;
GuardSeqN ->
BodyN % no semicolon at the end of the last body
end
if you intend to use this, beware that you must always have at least one guard that is true, otherwise the code will crash.
My version [edit: remove useless function, better print]:
-module (toh).
-export([start/1]).
start(N) ->
Game = #{1 => lists:seq(1,N), 2 => [], 3 => []},
display(Game,N),
move(N,Game,1,3,N).
move(1,Game,From,To,Size) ->
[H|NewFrom] = maps:get(From,Game),
NewTo = [H|maps:get(To,Game)],
NewGame = maps:update(From,NewFrom,maps:update(To,NewTo,Game)),
display(NewGame,Size),
NewGame;
move(N,Game,From,To,Size) ->
Other = other(From,To),
Game1 = move(N-1,Game,From,Other,Size),
Game2 = move(1,Game1,From,To,Size),
move(N-1,Game2,Other,To,Size).
display(#{1 := A, 2 := B, 3 := C},D) ->
lists:foreach(fun(X) -> print(X,D) end,lists:zip3(complete(A,D),complete(B,D),complete(C,D))),
io:format("~n~s~n~n",[lists:duplicate(6*D+5,$-)]).
complete(L,D) -> lists:duplicate(D-length(L),0) ++ L.
print({A,B,C},D) -> io:format("~s ~s ~s~n",[elem(A,D),elem(B,D),elem(C,D)]).
elem(I,D) -> lists:duplicate(D-I,$ ) ++ lists:duplicate(I,$_) ++ "|" ++ lists:duplicate(I,$_) ++ lists:duplicate(D-I,$ ).
other(I,J) -> 6-I-J.
In the shell:
Eshell V6.1 (abort with ^G)
1> c(toh).
{ok,toh}
2> toh:start(3).
_|_ | |
__|__ | |
___|___ | |
-----------------------
| | |
__|__ | |
___|___ | _|_
-----------------------
| | |
| | |
___|___ __|__ _|_
-----------------------
| | |
| _|_ |
___|___ __|__ |
-----------------------
| | |
| _|_ |
| __|__ ___|___
-----------------------
| | |
| | |
_|_ __|__ ___|___
-----------------------
| | |
| | __|__
_|_ | ___|___
-----------------------
| | _|_
| | __|__
| | ___|___
-----------------------
#{1 => [],2 => [],3 => [1,2,3]}
3>

Related

Implement a Photoshop Photo Filter on CSS

I want to recreate the result of Photoshop Photo Filters on css but not really sure which mix-blend-mode to use and how.
I know this is a fairly trivial task, but I still need a little hint.
Ideally, I would like to combine these two filters into one. Here are the filter details and some test colors
R E S U L T C O L O R S
+----------+ +----------+
| | | |
| f0f4e7 | | eac91a |
| | | |
+----------+ +----------+
^
Photo Filter 2 Yellow (f9e31c)
15% no preserve luminosity
^
Photo Filter 1 Underwater (00c2b1)
8% no preserve luminosity
^
I N I T I A L C O L O R S
+----------+ +----------+
| | | |
| ffffff | | ebca1c |
| | | |
+----------+ +----------+

Parse data in Kusto

I am trying to parse the below data in Kusto. Need help.
[[ObjectCount][LinkCount][DurationInUs]]
[ChangeEnumeration][[88][9][346194]]
[ModifyTargetInLive][[3][6][595903]]
Need generic implementation without any hardcoding.
ideally - you'd be able to change the component that produces source data in that format to use a standard format (e.g. CSV, Json, etc.) instead.
The following could work, but you should consider it very inefficient
let T = datatable(s:string)
[
'[[ObjectCount][LinkCount][DurationInUs]]',
'[ChangeEnumeration][[88][9][346194]]',
'[ModifyTargetInLive][[3][6][595903]]',
];
let keys = toscalar(
T
| where s startswith "[["
| take 1
| project extract_all(#'\[([^\[\]]+)\]', s)
);
T
| where s !startswith "[["
| project values = extract_all(#'\[([^\[\]]+)\]', s)
| mv-apply with_itemindex = i keys on (
extend Category = tostring(values[0]), p = pack(tostring(keys[i]), values[i + 1])
| summarize b = make_bag(p) by Category
)
| project-away values
| evaluate bag_unpack(b)
--->
| Category | ObjectCount | LinkCount | DurationInUs |
|--------------------|-------------|-----------|--------------|
| ChangeEnumeration | 88 | 9 | 346194 |
| ModifyTargetInLive | 3 | 6 | 595903 |

Can somebody explain dbms_sql.number_table [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I tried to search this but could not get a satisfactory answer hence posting here . somebody please explain
Better to understand DBMS_SQL itself to some extent, before understanding NUMBER_TABLE.
( I do this for My Learning!)
NUMBER_TABLE
is Actually,
TYPE number_table IS TABLE OF NUMBER INDEX BY BINARY_INTEGER;
So, only numbers are allowed!
FlowChart on How DBMS_SQL Works! :
Your interested area comes in bind variable box
-- | open_cursor |
-- -----------
-- |
-- |
-- v
-- -----
-- ------------>| parse |
-- | -----
-- | |
-- | | ---------
-- | v |
-- | -------------- |
-- |-------->| bind_variable | |
-- | ^ ------------- |
-- | | | |
-- | -----------| |
-- | |<--------
-- | v
-- | query?---------- yes ---------
-- | | |
-- | no |
-- | | |
-- | v v
-- | ------- -------------
-- |----------->| execute | ->| define_column |
-- | ------- | -------------
-- | |------------ | |
-- | | | ----------|
-- | v | v
-- | -------------- | -------
-- | ->| variable_value | | ------>| execute |
-- | | -------------- | | -------
-- | | | | | |
-- | ----------| | | |
-- | | | | v
-- | | | | ----------
-- | |<----------- |----->| fetch_rows |
-- | | | ----------
-- | | | |
-- | | | v
-- | | | -----------------
-- | | | | column_value |
-- | | | | variable_value |
-- | | | -----------------
-- | | | |
-- | |<--------------------------
-- | |
-- -----------------|
-- |
-- v
-- ------------
-- | close_cursor |
--
------------
Example:
In a DELETE statement, for example, you could bind in an array in the WHERE clause and have the statement be run for each element in the array:
DECLARE
stmt VARCHAR2(200);
dept_no_array DBMS_SQL.NUMBER_TABLE;
c NUMBER;
dummy NUMBER;
begin
dept_no_array(1) := 10; dept_no_array(2) := 20; /* Put some values into the array */
dept_no_array(3) := 30; dept_no_array(4) := 40;
dept_no_array(5) := 30; dept_no_array(6) := 40;
stmt := 'delete from emp where deptno = :dept_array'; /* A Dynamic SQL String with a bind variable */
c := DBMS_SQL.OPEN_CURSOR; /* Open a Cursor! */
DBMS_SQL.PARSE(c, stmt, DBMS_SQL.NATIVE); /* Parse the Dynamic SQL , making it happen on the native database to which is connected! */
DBMS_SQL.BIND_ARRAY(c, ':dept_array', dept_no_array, 1, 4);
/* Bind only elements 1 through 4 to the cursor Happens 4 times */
dummy := DBMS_SQL.EXECUTE(c);
/* Execute the Query, and return number of rows deleted! */
DBMS_SQL.CLOSE_CURSOR(c);
EXCEPTION WHEN OTHERS THEN
IF DBMS_SQL.IS_OPEN(c) THEN
DBMS_SQL.CLOSE_CURSOR(c);
END IF;
RAISE;
END;
/
P.S. Pure rip-off, with some more commments ,from Oracle

Drupal business listing a-z indexing requirements

I want to render something like below in a view.
==================================================================
View: Car parts (Content type)
A | [B] | C | D | E | [F] | [G] | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z
BONNET
FRONT BUMPER
FRONT BAR REINFORCEMENT
GUARD LEFT
GUARD RIGHT
==================================================================
How do you create the top A – Z index. The letters in [ ] are anchor links.
I think this discussion could help you:
https://drupal.org/node/403012

Testcase level variables in [Documentation] for robot framework

I cannot get test level variables to appear in documentation.
Let's say I have this testsuite:
| *Variables* |
| ${SystemUnderTest} = | Staging
| *testcase* |
| Device Test |
| | Set Test Variable | ${device} | iPhone
| | [Documentation] | Device is: ${device} |
| | ... | System is: ${SystemUnderTest} |
| | No Operation
That produces this log:
TEST CASE: Device TestExpand All
Full Name: T.Device Test
Documentation:
Device is: ${device} System is: Staging
Notice that the Suite level variable is treated properly, but the test level one is not.
How do I get all variables to be treated equally?
Starting with robotframework 2.7 there is a built-in keyword named Set test documentation, which can be used to replace or append to the existing documentation. This will not affect the output in the console, but the changes will be reflected in the log and report.
For example:
| *Variables* |
| ${SystemUnderTest} = | Staging
| *testcase* |
| Device Test |
| | Set Test Variable | ${device} | iPhone
| | [Documentation] | Device is: ${device} |
| | ... | System is: ${SystemUnderTest} |
| | Substitute vars in documentation
| | No Operation
| *Keywords* |
| Substitute vars in documentation
| | ${doc}= | replace variables | ${test documentation}
| | set test documentation | ${doc}
For more information see http://robotframework.googlecode.com/hg/doc/libraries/BuiltIn.html?r=2.7.7#Set%20Test%20Documentation
This solution feels a little hackerish to me, but it does give you the functionality that you want.
Test.txt
| *Setting* | *Value* |
# This should start as the value for your first test
| Suite Setup | Set Suite Variable | ${device} | foo
| *Test Case* | *Action* | *Argument*
#
| T100 | [Documentation] | Should be foo: ${device}
# Do some stuff
| | No Operation
# This setups the device name for the next test.
| | Set Suite Variable | ${device} | bar
#
| T101 | [Documentation] | Should be bar: ${device}
# Do some stuff
| | No Operation
| | Set Suite Variable | ${device} | bing
#
| T102 | [Documentation] | Should be bing: ${device}
# Do some stuff
| | No Operation
When I run the suite I get this output:
==============================================================================
Test
==============================================================================
T100 :: Should be foo: foo | PASS |
------------------------------------------------------------------------------
T101 :: Should be bar: bar | PASS |
------------------------------------------------------------------------------
T102 :: Should be bing: bing | PASS |
------------------------------------------------------------------------------
Test | PASS |
3 critical tests, 3 passed, 0 failed
3 tests total, 3 passed, 0 failed
==============================================================================
Having the device variable set at the end of the previous test is a little unclean, but as long you leave a comment it shouldn't be unclear at all.

Resources