Post Reply 
Replica a number
02-03-2022, 10:16 AM
Post: #1
Replica a number
Good morning,
Is there in HP Prime a command that replaces a series of numbers with other numbers?
For example:
{1,1,0,0,0,1} --comand--> {1,1,2,2,2,1}
all zeroes replaced by 2.

Thak you, Roberto
Find all posts by this user
Quote this message in a reply
02-03-2022, 10:20 AM
Post: #2
RE: Replica a number
(02-03-2022 10:16 AM)robmio Wrote:  Good morning,
Is there in HP Prime a command that replaces a series of numbers with other numbers?
For example:
{1,1,0,0,0,1} --comand--> {1,1,2,2,2,1}
all zeroes replaced by 2.

Thak you, Roberto

Another examle:

{1,1,0,1,0,0,1,1,0,1} --》{1,1,2,1,2,2,1,1,2,1}
Find all posts by this user
Quote this message in a reply
02-03-2022, 01:43 PM
Post: #3
RE: Replica a number
I don't think there is a built in command to do this but it can be done with a simple program such as this one which will replace in list each occurrence of num1 by num2 (it works only for numbers, not for other types of list elements):

Code:
EXPORT ReplNum(list,num1,num2)
BEGIN
 EXECON("IFTE(&1=="+num1+","+num2+",&1)",list);
END;
Find all posts by this user
Quote this message in a reply
02-03-2022, 01:52 PM
Post: #4
RE: Replica a number
Good day,
there is no command to perform an operation like this, but you can make one:
MAKELIST(when((L1(X)) = 1,2,L1(X)),X,1,SIZE(L1),1), here the list you want to change is stored in L1, with
L1:=MAKELIST(when((L1(X)) = 1,2,L1(X)),X,1,SIZE(L1),1)
you replace the actually stored list.
Arno
Find all posts by this user
Quote this message in a reply
02-03-2022, 02:04 PM
Post: #5
RE: Replica a number
(02-03-2022 10:20 AM)robmio Wrote:  
(02-03-2022 10:16 AM)robmio Wrote:  Good morning,
Is there in HP Prime a command that replaces a series of numbers with other numbers?
For example:
{1,1,0,0,0,1} --comand--> {1,1,2,2,2,1}
all zeroes replaced by 2.

Thak you, Roberto

Another examle:

{1,1,0,1,0,0,1,1,0,1} --》{1,1,2,1,2,2,1,1,2,1}

Great: I used a "for" loop: it works, but it slows down the execution of my other programs. Your solution is quick and easy: thank you very much.
Find all posts by this user
Quote this message in a reply
02-03-2022, 02:07 PM
Post: #6
RE: Replica a number
(02-03-2022 01:43 PM)Didier Lachieze Wrote:  I don't think there is a built in command to do this but it can be done with a simple program such as this one which will replace in list each occurrence of num1 by num2 (it works only for numbers, not for other types of list elements):

Code:
EXPORT ReplNum(list,num1,num2)
BEGIN
 EXECON("IFTE(&1=="+num1+","+num2+",&1)",list);
END;

Great: I used a "for" loop: it works, but it slows down the execution of my other programs. Your solution is quick and easy: thank you very much.
Find all posts by this user
Quote this message in a reply
02-03-2022, 02:10 PM
Post: #7
RE: Replica a number
(02-03-2022 01:52 PM)Arno K Wrote:  Good day,
there is no command to perform an operation like this, but you can make one:
MAKELIST(when((L1(X)) = 1,2,L1(X)),X,1,SIZE(L1),1), here the list you want to change is stored in L1, with
L1:=MAKELIST(when((L1(X)) = 1,2,L1(X)),X,1,SIZE(L1),1)
you replace the actually stored list.
Arno

Your solution is also faster than my "for" loop. Thanks so much
Find all posts by this user
Quote this message in a reply
02-08-2022, 11:34 AM
Post: #8
RE: Replica a number
Bonjour
Ces 2 solutions parcourent toute la liste des valeurs, sur de grandes listes où il y a peu de valeurs à remplacer cela peut prendre du temps inutilement.
Il y a une autre solution qui consiste à rechercher les valeurs à remplacer puis à arrêter quand il n'y on a plus.
A tester !

Hello
These 2 solutions go through the whole list of values, on big lists where there are few values to replace it can take time unnecessarily.
There is another solution which consists in searching for the values to replace and then stop when there are no more.
To be tested!

Code:

EXPORT LCHG(l,a,n)
//Liste,élément,élément
BEGIN
LOCAL i;
REPEAT 
 i:=POS(l,a);
 IF i THEN
  l(i):=n;
 END;
UNTIL i==0;
l;
END;

Sorry for my english
Find all posts by this user
Quote this message in a reply
02-08-2022, 12:50 PM
Post: #9
RE: Replica a number
(02-08-2022 11:34 AM)Tyann Wrote:  Bonjour
Ces 2 solutions parcourent toute la liste des valeurs, sur de grandes listes où il y a peu de valeurs à remplacer cela peut prendre du temps inutilement.
Il y a une autre solution qui consiste à rechercher les valeurs à remplacer puis à arrêter quand il n'y on a plus.
A tester !

Hello
These 2 solutions go through the whole list of values, on big lists where there are few values to replace it can take time unnecessarily.
There is another solution which consists in searching for the values to replace and then stop when there are no more.
To be tested!

Code:

EXPORT LCHG(l,a,n)
//Liste,élément,élément
BEGIN
LOCAL i;
REPEAT 
 i:=POS(l,a);
 IF i THEN
  l(i):=n;
 END;
UNTIL i==0;
l;
END;

Excellent "code": very fast.
Find all posts by this user
Quote this message in a reply
Post Reply 




User(s) browsing this thread: 1 Guest(s)