Recent

Author Topic: Help me make a program please..  (Read 18309 times)

irppaann

  • Newbie
  • Posts: 2
Help me make a program please..
« on: July 18, 2017, 12:53:24 pm »
so i got a task from my lecturer, im a freshyear, so skip it all and this is the task
i should make a program that could remove a double alphabet or string ( im sorry if you dont understand what im saying im not very good with english)
anyway let me give you an example how the program works

INPUT = asda
OUTPUT = sd

so any alphabet that has double on it should be removed. can someone help me?
im confuse with what syntax im should use for.. oh btw im using pascal for those who wondering..
« Last Edit: July 18, 2017, 01:01:28 pm by irppaann »

Thaddy

  • Hero Member
  • *****
  • Posts: 14169
  • Probably until I exterminate Putin.
Re: Help me make a program please..
« Reply #1 on: July 18, 2017, 01:20:36 pm »
Homework again.
Try reading about sets of char, include, exclude, in
Specialize a type, not a var.

irppaann

  • Newbie
  • Posts: 2
Re: Help me make a program please..
« Reply #2 on: July 18, 2017, 01:23:08 pm »
Homework again.
Try reading about sets of char, include, exclude, in

okay thanks ill see it

Handoko

  • Hero Member
  • *****
  • Posts: 5122
  • My goal: build my own game engine using Lazarus
Re: Help me make a program please..
« Reply #3 on: July 18, 2017, 02:38:38 pm »
Hello irppaann.

We would like to help, but you should show us what you've already written. For your information, your task can be solved using many different ways. Without knowing what you have written and not knowing what you've already learned from the school, the solution we give you may not the one your teacher want, s/he will think you asked someone to do your homework.

Thaddy

  • Hero Member
  • *****
  • Posts: 14169
  • Probably until I exterminate Putin.
Re: Help me make a program please..
« Reply #4 on: July 18, 2017, 03:51:26 pm »
Homework again.
Try reading about sets of char, include, exclude, in

okay thanks ill see it

It is all you need to solve your homework. Trust me... It was an honest answer.... I teached. What your teacher wants is for you to use set operations.
It is all in the manuals: https://www.freepascal.org/docs-html/current/ref/refsu16.html#x40-580003.3.3 http://www.tutorialspoint.com/pascal/pascal_sets.htm and many more.
You need two sets to solve it (use in) . And two operations afterwards to solve it...
« Last Edit: July 18, 2017, 04:09:38 pm by Thaddy »
Specialize a type, not a var.

wp

  • Hero Member
  • *****
  • Posts: 11832
Re: Help me make a program please..
« Reply #5 on: July 18, 2017, 04:10:27 pm »
How would you solve this task manually, without a computer? You would run through the string character by character and count how often each character is present. Then you would run through the string a second time and pick those characters with an abundance = 1. Now it's your job to translate these operations to Pascal language.

Thaddy

  • Hero Member
  • *****
  • Posts: 14169
  • Probably until I exterminate Putin.
Re: Help me make a program please..
« Reply #6 on: July 18, 2017, 04:12:28 pm »
How would you solve this task manually, without a computer? You would run through the string character by character and count how often each character is present. Then you would run through the string a second time and pick those characters with an abundance = 1. Now it's your job to translate these operations to Pascal language.

 :o Even you could do with picking up on intersection and exclusion/inclusion  :D ;D Just teasing, but that's the same as I wrote.. MUST be enough...

(As an aside: I really do not know many forum members who really use all the power of sets in a proper way.... We should make that a contest! -but not now! Afterwards)
« Last Edit: July 18, 2017, 04:19:01 pm by Thaddy »
Specialize a type, not a var.

wp

  • Hero Member
  • *****
  • Posts: 11832
Re: Help me make a program please..
« Reply #7 on: July 18, 2017, 04:34:38 pm »
Thaddy, the OP is not seeking the BEST or most INTERESTING or NICEST solution, he is seeking for ANY solution which is working and which he understands.

cpicanco

  • Hero Member
  • *****
  • Posts: 618
  • Behavioral Scientist and Programmer
    • Portfolio
Re: Help me make a program please..
« Reply #8 on: July 18, 2017, 07:32:26 pm »
Should the program remove any duplicated chars?

input:

aaword

aworda

wordaa

awaord

output:

word
Be mindful and excellent with each other.
https://github.com/cpicanco/

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 9754
  • Debugger - SynEdit - and more
    • wiki
Re: Help me make a program please..
« Reply #9 on: July 18, 2017, 09:54:29 pm »
Well do not read about array. Unless your teacher did talk about array.

Quote
INPUT = asda
OUTPUT = sd

You should know how to look at each character. (Your teacher must have told you about "for" loops)

1) do a loop and look at each char
LOOK at the first char 'a'

2)
do another loop, and look at each char again.
compare each char to the 'a' and count.
You get:2
So skip it.


Next round in the loop 1

This time you get an 's'
and your second loop should count to 1
and you copy the 's'

-------------------
write this in pascal.
even if you have errors, and even if it does not compile.

Post/write your attempt here.
« Last Edit: July 18, 2017, 09:56:18 pm by Martin_fr »

Bart

  • Hero Member
  • *****
  • Posts: 5265
    • Bart en Mariska's Webstek
Re: Help me make a program please..
« Reply #10 on: July 19, 2017, 12:51:41 am »
It should be possible in just one loop ...

Bart

sam707

  • Guest
Re: Help me make a program please..
« Reply #11 on: July 19, 2017, 08:43:07 pm »
 :D Bart, do it in one only loop and I eat my shoes instant haha

you can minimize the loops count by using a 'Set of char' as tester, instead of having a loop per char-1, but you can't do it in 1 only loop!

sometimes problems seem easy ... but.... hehehehehe
« Last Edit: July 19, 2017, 08:46:36 pm by sam707 »

rvk

  • Hero Member
  • *****
  • Posts: 6056
Re: Help me make a program please..
« Reply #12 on: July 19, 2017, 08:55:37 pm »
.. but you can't do it in 1 only loop!
If you look at every character (one loop) you can check with Pos(character, Copy(string, I + 1)) if that character is also in the rest of the string. If it is, you can use StringReplace to replace that character with an empty string (and don't increment I). Voila, one loop :D

Technically StringReplace itself contains a loop but if you count that as a loop you'll have to count the whole source of FPC too.

Bart

  • Hero Member
  • *****
  • Posts: 5265
    • Bart en Mariska's Webstek
Re: Help me make a program please..
« Reply #13 on: July 19, 2017, 09:29:18 pm »
:D Bart, do it in one only loop and I eat my shoes instant haha

OK, but you wil have to upload a video to proof yuo actually did that  >:D

This one loop programm eliminates every charater that is used more than 1 time in the input string.
(Or at least it should, it may be bugged, but the one loop priciple stands.)

Code: Pascal  [Select][+][-]
  1. {$Mode ObjFpc}
  2.  
  3. uses
  4.   sysutils;
  5.  
  6. const
  7.   S = 'the quick brown fox jumped over the lazy dog';
  8. var
  9.   i: Integer;
  10.   Eliminated: TSysCharSet;  //TSysCharSet = Set of AnsiChar;
  11.   Res: String;
  12.   C: Char;
  13.  
  14. function ShowCharSet(ASet: TSysCharSet): String;
  15. begin
  16.   Result := '[';
  17.   for C in ASet do
  18.   begin
  19.     if (C > #32) and (C < #128) then
  20.       Result := Result + C + ', '
  21.     else
  22.     Result := Result + '#' + IntToStr(Ord(C)) + ', ';
  23.   end;
  24.   if (Length(Result) > 2) then Delete(Result, Length(Result)-1, 2);
  25.   Result := Result + ']';
  26. end;
  27.  
  28. begin
  29.   Eliminated := [];
  30.   Res := '';
  31.   for i := 1 to length(S) do
  32.   begin
  33.     C := S[i];
  34.     if (not (C in Eliminated)) then
  35.     begin
  36.       if (Pos(C, Copy(S, i+1, MaxInt)) > 0) then
  37.       Eliminated := Eliminated + [C];
  38.       if not (C in Eliminated) then
  39.         Res := Res + C;
  40.     end;
  41.   end;
  42.   writeln('Input:  ',S);
  43.   writeln('Output: ',Res);
  44.   writeln('Eliminated: ', ShowCharSet(Eliminated));
  45. end.
  46.  

Outputs:
Code: [Select]
[bart@simenon ConsoleProjecten]$ ./test
Input:  the quick brown fox jumped over the lazy dog
Output: qickbwnfxjmpvlazyg
Eliminated: [#32, d, e, h, o, r, t, u]

(Yes, there are 2 loops, but the ShowCharSet function is just superfluous eye-candy)

Bart
« Last Edit: July 19, 2017, 09:31:53 pm by Bart »

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 9754
  • Debugger - SynEdit - and more
    • wiki
Re: Help me make a program please..
« Reply #14 on: July 19, 2017, 09:33:36 pm »
"Pos" is a loop too.

The fact that you didnt write it in your own code, but used a loop implemented in a library does not matter.

It is 2 loops.

---
You can do recursion instead of loop though.
« Last Edit: July 19, 2017, 09:36:35 pm by Martin_fr »

 

TinyPortal © 2005-2018