cancel
Showing results for 
Search instead for 
Did you mean: 
JKova
Level 4
138 92 2 5
Message 1 of 11
1,090
Flag Post

Solved!

HP 50g How to detect duplicate values while entering INFORM input repeatedly

HP Recommended
HP 50g graphing calculator

Hi dear HP Community,

Before i went to topic i hope that you'll safe and sound.

 

So here is my issue,

I want to write a code which will able to detect duplicate values ( and show a message when value is detected as 

duplicate ( or multiple same value )) when repeatedly entering those values as INFORM input.

Also i want to store all entered values in list and when happens that some of entered values turns out to be duplicate ( or multiple ) I want to RE-ENTER that value , or these values if I have more of this , and store it in place ( in list )  where that duplicate value was initially.

 

So right above is definition of my task , hope you understand what I'm asking.

Also I'm referring to write this code for HP 50g graphing calculator  and if possible in User RPL Language.

 

Here is a code , "bug" code of what i done so far but unfortunately something is wrong with my code.

 

« 0. 'ZAK' STO 0. 'brojac' STO { } 'LIST' STO

  1. 10.
  FOR i
  1. 'brojac' STO+
  "INFORM"
   { { "Input a number or string" "" 0. 2. 6. 9. } }
   { 1. 1. } { } { } INFORM DROP OBJ→ DROP
    IF i 2. ≥
    THEN 'B' STO B 'LIST' STO+ @entered value store in list 'LIST'
    ELSE 'A' STO A 'LIST' STO+ @entered value store in list 'LIST'
    END 1. LIST SIZE 1. -
    FOR j
      IF LIST SIZE 2. ≥
      THEN j 1. + LIST SIZE
        FOR k
          IF LIST j GET LIST k GET SAME @this part check if value is duplicated
          THEN 1. 'ZAK' STO @ this variable is used for detection of duplicate value
          ELSE
          END
        NEXT
      END
    NEXT

    0. 'broji' STO @ counter for deleting element of list when duplicated number detected
    IF ZAK 1. == i 2. ≥ AND
    THEN 0. 'ZAK' STO
         1. 'broji' STO+
         "Duplicate number or string detected !!
          Please enter again !!"
          MSGBOX 1. LIST REVLIST AXL i COL- DROP AXL REVLIST 'LIST' STO
          "RE ENTER VALUE AGAIN"
          { { "Input a number or string" "" 0. 6. 9. } }
          { 1. 1. } { } { } INFORM DROP OBJ→ DROP 'A' STO A 'LIST' STO+
    END
  NEXT
»

 

Try this code on yourself !!

Problem with this code is in fact when you enter multiple values , like as example 1 , 1 , 1 , 1 in INFORM input the value which is entered as second ( 2. nd ) will be recognized as duplicate but when you hit that number ( 1 ) at third ( 3. rd ) time then value does not be recognized as duplicate. Again when you enter same number ( 1 ) ( or string but does not work on any string ) then you'll get a message which signalize a duplicate... so ...  not so good algorithm.

 

Also my program does not works on any strings and that is bug too because i want to recognize duplicate in quite sort of data types including numbers, strings and global variable name NOT only numbers !!

 

Please help me with this because i need this for my program !!

Bye 🙂

 

0 Kudos
10 REPLIES 10
cyrille
Level 6
Level 6
252 251 42 77
Message 2 of 11
Flag Post
HP Recommended

Hello,

 

I have not looked at your program in details... but you should use the POS function which, if I remember well will tell you where a value is in the list (or if it is not there).. It might make your life a whole lot easier!

 

Cyrille

I am an HP Employee
David_M
Level 6
140 139 29 60
Message 3 of 11
Flag Post
HP Recommended

Suggestion:

 

Instead of saving every input value to your list and then checking for duplicates, think of your list in a different way.  Check for the current value supplied in the loop as being a duplicate before adding it to the list (using POS as Cyrille mentioned), and only add it to the list if it is unique.  That way the only values that are ever added to the list are fully qualified, and will already be in the proper order.

 

A side note:

 

Be careful with SAME and POS when it comes to comparing exact numbers and approximate numbers.  Both of those commands see "1." and "1" as being different.  "==" will see them as being equivalent, but you'd have to come up with a POS replacement if you want it to see those as being the same for a match.  A better approach might be to make sure that all numeric values entered are translated to approximate numbers by executing I→R on the input.  One way to accomplish that is to use IFERR I→R THEN END, which will ignore any objects that aren't exact numbers.

 

Hope this helps!

cyrille
Level 6
Level 6
252 251 42 77
Message 4 of 11
Flag Post
HP Recommended

Hello,

 

it has been along time since I have done RPL, but your code should look somethign like this:

<<

{} -> L <<

while L SIZE 10 <> DO

  IF { input params } INPUT

  THEN

   IF L OVER POS THEN "error" MSGBOX

   ELSE 'L' STO+

   END

  END

END

>>

>>

I am an HP Employee
0 Kudos
JKova
Author
Level 4
138 92 2 5
Message 5 of 11
Flag Post
HP Recommended

Hi Cyrille,

 

Unfortunatelly your code does not work ,

Don't know if you try your own code in emulator
or physically on your HP 50g graphing calculator
😕

 

Also i refer on "INFORM" function ( or command )
as input option , "INPUT" function is not
compatibile with that what should i get.

 

See the video : 

 

video link 

 

I try this on emulator ( like video ) and on my HP 50g graphing calculator - but i get error on two sides.

 

Also i refer on entering strings , numbers , and maybe , if possible - global variable name.

Don't know if you keep this in mind.

 

Anyway thanks for try 😊

 

 

 

0 Kudos
cyrille
Level 6
Level 6
252 251 42 77
Message 6 of 11
Flag Post
HP Recommended

Hello,

 

I know that it does not work as is.. it is pseudocode, ie, something that looks, at first glance, like code, but is not...

The aim of pseudocode is to comunicate an idea, an algo, an organization... but it does not try to communicate the details of implementation...

 

Here, the important parts are:

the while list size!=10 part as the main loop

The use of POS to only add the new data in the list if it is not already there. with the use of the fact that POS returns 0 if not found and that 0 is a false for IF structures... (Note: in the code, I messed up the THEN and ELSE clauses, they need to be swapped)

the handling of the cancel in the INPUT and the handling of error when the user enters the same same data twice.

 

The point is that using the structure that I provided above, you should be able to generate a much simpler version of your code and a version with much less troubles.

 

Cyrille

I am an HP Employee
JKova
Author
Level 4
138 92 2 5
Message 7 of 11
Flag Post
HP Recommended

Hi Cyrille , 

 

I really does not understand your code even less pseudocode , 

You should maybe send me some kind of Flowchart maybe will be better for understanding and maybe to discuss about that theme. 

Sorry for misunderstanding  about your message before your previous message, but when you type code type it in that way i immediately think that must be code , even so You used function that HP 50g already have.

Don't know how it's made psudocode in America but we in Croatia do that on not so similar way.

 

Second thing , I use POS function like you mention in your first message on this topic and I came up with longer solution for this problem and works exactly like i planned, so thanks brother Cyrille, and thanks to David_M because i came up with code , Ill post my code below as a picture.

 

«
"Enter a quantity of points "
{ { "n points" "" 0 } }
{ 1 1 } { } { }
INFORM DROP OBJ→ DROP 'nt' STO

{ } 'lista' STO
1 nt FOR i
0 'brojilo' STO
"Point " i →STR +
{ { "ID" "Enter ID of point" 0 2 } }
{ 1 1 } { } { }
INFORM DROP OBJ→ DROP 'valueA' STO
valueA 'lista' STO+
IF lista REVLIST valueA POS i ≠ THEN
"You enter same value like before !! Please enter different value"
MSGBOX
DO
1 'brojilo' STO+ @ brojilo - is counter for "frivolity"
IF brojilo 2 ≥ THEN
"You're kidding me , right ?! 😮 Enter different value" MSGBOX
END
"Enter ID of point AGAIN"
{ { "ID" "Enter correctly ID of point" 0 2 } }
{ 1 1 } { } { }
INFORM DROP OBJ→ DROP 'valueB' STO

0 'provjera' STO
1 lista SIZE
FOR k
IF lista REVLIST k GET valueB SAME
THEN 1 'provjera' STO @ checking part of algorithm which checks duplicates
END
NEXT

UNTIL
provjera 0 SAME @here you enter ID so many times until you entered ID correctly
END
lista REVLIST i valueB 1 →LIST REPL REVLIST 'lista' STO
END
NEXT
»

 

Here is an explanation what was my intentions with this code.

So first part is defining a number of points which will be saved into variable nt , then i created empty list for storing initially values of ID when type ID on each of this point. Initially value of point ID is stored on variable which i called "valueA" , then i stored those variable in list of all ID's of my points and i called that list "lista" . After previoues things which i described are done then algorithm checks if position of entered value differs from position of entered value , I use fact that when you hit the POS function and value in list you'll only get first position of that value no matter how many these "duplicated" values are... for example if i have this list { 1 2 3 3 3 4 5 } and when i type 3 POS I'll get 3 as a position of my value on stack of HP 50g graphing calculator. if counter i differs from position of that value it's clearely detected as duplicate, then message appears and message informs you about that duplicate value and gives you opportunity to enter that value ( ID of point is value ) once again. provjera is variable that checks if value is duplicate or not , and if entered value was duplicate then provjera gets a value of 1 ,  if  not duplicate then gets value of 0. Also this previously part of code goes trough all values entered in list and when found a duplicate then variable provjera makes sence in those code , i did not mentioned this but for sure is clearely seen.

If you enter variable in a right way  then element of list which is duplicate gets replaced by value which is correctly entered , i store this value like valueB , that is this part of my code :

 

lista REVLIST i valueB 1 LIST REPL REVLIST 'lista' STO

 

Here is a pictures in HP UserEdit , on two parts because it did not fit in one window.

 

Part_of_algorithm_A.png

 

Part_of_algorithm_B.png

 

 

Yeah this is much longer version as you intented to do - Cyrille , I want to do shorter version like you mentioned in a mesaage before but i really don't know each of function/command which you use in your "pseudocode" for HP 50g graphing calculator, so if you have solution for implementation or really code - I can't wait to see that. 

 

Bye and have a nice day 😉 

 

 

 

0 Kudos
JKova
Author
Level 4
138 92 2 5
Message 8 of 11
Flag Post
HP Recommended

Hi Cyrille , 

I rewrite your function and replace some functions with some mine's , but not big problem.

Only thing that really matters is implementation because ... cause i don't put pseudocode into machine while programming , so only code really matters !!!

 

So here is my solution , it works in HP 50g graphing calculator/emulator :

«
{} 'L' STO @instead of creating local variable i create global variable type
WHILE L SIZE 10 <
  REPEAT
  "Variable name?"
  { { "var" "" 0 2 } }
  { 1 1 } { } { }
  INFORM DROP OBJ→ DROP     @instead of INPUT i used INFORM function

   IF L OVER POS THEN "error" MSGBOX DROP @ i use DROP function because duplicate 
                                          @ value , when found , is displayed on stack
   ELSE 'L' STO+
   END

  END
»

 

here is a picture of that code :

 

debugging_code.png

 

I must to mention that i don't know if this code works with local variable type so instead of local variable type i used global

variable type because when you use global variable type you can add some things , like in case of list when you add elements of list with the STO+ function. 

 

Anyway thanks for help , i really appreciate this , so no matter what implementation missed.

And sorry if my english was in bad condition.

 

Bye 🤗

 

 

 

 

0 Kudos
cyrille
Level 6
Level 6
252 251 42 77
Message 9 of 11
Flag Post
HP Recommended

Hello,

 

Sorry the pseudocode disturbed you...

 

Flowcharts can be usefull, however, the are not usable in "real life" scenarios because they quickly become way too large, while text based stuff still fits in 1 page...

 

However, looking at your latest code, I can see that it did what it was intended to, you were able to understand the meaning and put it into practice...

 

A couple of notes though:

if the user clicks Cancel on the dialog box, then you are not handling this case (INFORM returns 0)

there is no problem using STO+ on a local variable, it will work... and avoids the mess that a global variable can cause.

 

Cyrille

I am an HP Employee
JKova
Author
Level 4
138 92 2 5
Message 10 of 11
Flag Post
HP Recommended

Hi Cyrille , 

 

What i read from your previouse message i came to conclusion that your behaviour is very decent.

Sorry for disturbing you , yeah 🤣

So , yeah flowcharts are too big for some complex code(s) or operation(s), yeah i agree with you about that.

 

So , you mention that i can do that type of code with local variable type , also i learn some new about that... cause 

I did not believe that you can use STO+ function on local variables , i thought that is only possible to do with global variable types. 

 

I saw that fact  in your last message ...

 


@cyrille wrote:

A couple of notes though:

if the user clicks Cancel on the dialog box, then you are not handling this case (INFORM returns 0)

there is no problem using STO+ on a local variable, it will work... and avoids the mess that a global variable can cause.

 

Cyrille


 

So i decided to make this happen with local variables and i think that i did it.

See below ,

 

«
{} → L @ NOW WITH LOCAL VARIABLE TYPE
«
WHILE L SIZE 10 <
  REPEAT
    "Input"
   { "Input" "" { 1 0 } V } INPUT OBJ→ @ NOW IT WORKS FOR INPUT  !!

   IF L OVER POS THEN "error" MSGBOX DROP @ I use DROP function because
                                          @ duplicate value , when found ,
                                          @ is displayed on stack
   ELSE 'L' STO+
   END
  END
  L →NUM 'LISTA' STO @ I add this part of program to save list in memory

»
»

 

here is how this looks when you type all of this in HP UserEdit program ( picture down below , i think )

 

Cyrille_you_make_this_to_happen.png

 

After all of this help and explanations i can only  say to you - Thank you !!

I'm really appreciate your time and willingness to make this code happen, also i accept your solution by the way 

and wish you a pleasant day.

 

Bye 🤗

 

 

0 Kudos
† The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation