The Official Programming Thread

gullfounder

Phoenix Dawn
Supervisor
Aug 25, 2008
970
3
24
Khanpur
Another classic! I usually use the XOR method the:
Code:
a=a^b;
b=a^b;
a=a^b;
I was given this problum in my class. Whole class wasnt able to solve it. Only i solved. So i remember it to this day.

In tution i usually give this problum to my students to solve it. But looks like generation is soo dull. No has give me answer for it.
 

MegamanEXE

Well-known member
Dec 7, 2007
1,958
1
43
28
Islamabad
I was given this problum in my class. Whole class wasnt able to solve it. Only i solved. So i remember it to this day.

In tution i usually give this problum to my students to solve it. But looks like generation is soo dull. No has give me answer for it.
Most people won't. Even though there are those other ways, like the division/multiplication method, but I like this because it's simpler, especially for anyone knows what bits are.
 

gullfounder

Phoenix Dawn
Supervisor
Aug 25, 2008
970
3
24
Khanpur
Most people won't. Even though there are those other ways, like the division/multiplication method, but I like this because it's simpler, especially for anyone knows what bits are.
Programming is very hig field. But here in pakistan there is no place for it.

Expensive electeicity
Large taxs of on electronics
No awareness


A general question to all reader of this thread. What you guys think knowledge is important for programming ?
 

EternalBlizzard

Lazy guy :s
Moderator
Oct 29, 2011
2,732
1,193
129
Attractor Field Beta
@gullfounder

Wow i wasn't able to solve it afterall.... after scratching my head for 15 minutes, I had to look up on web and found this
Code:
x = 3
y = 4
x, y = y,x
print x           # prints 4
print y           # print 3
I never knew this type of value assignment existed....
:sorry: (it hurts to know you aren't a genius) :D anyways i looked up and also find bitwise operators... i read wiki but didn't understand much so what should i read before i can understand bits/bitwise operators.....
 
Last edited:

helraizer

Active member
Aug 13, 2013
414
0
21
okay i learned some basic of programming specifically python programming but i cant figure out oop its so confusing whenever i try to learn oop i end up quiting...

so my question wtf is oop and do we really need it?
 

MegamanEXE

Well-known member
Dec 7, 2007
1,958
1
43
28
Islamabad
okay i learned some basic of programming specifically python programming but i cant figure out oop its so confusing whenever i try to learn oop i end up quiting...

so my question wtf is oop and do we really need it?
It's... a way to write programs; a thinking approach to how to organize the code by thinking about it in terms of real life objects. An object has 2 things; its attributes (how it looks like, color, weight etc.) and what it can do (actions e.g a car can start, accelerate, slow down etc.). You combine these 2 and you have an object, which you do stuff on.... I dunno it's kinda weird to explain it in such a short way. Basically, you have a layout of how an object should look like and behave called a Class. From a class, you can make objects. You can like make a hundred or a thousand objects, and stuff. Then you can inherit the properties and specializes it.

In short, if you combine functions/methods & data types, you get a class.
 

EternalBlizzard

Lazy guy :s
Moderator
Oct 29, 2011
2,732
1,193
129
Attractor Field Beta
All right i have done the hangman... although it gives a weird error if i enter space instead of any letter and if i enter two letters at once? [MENTION=1394]MegamanEXE[/MENTION] does yours give any errors if you do this?
If you have time.... can you test this code and give me some feedback.. here's an online compiler
repl.it

Here is the code
Spoiler: show
Code:
words = ["glove", "emblem", "dragon", "hell", "pakistan", "rexcalibur", "london", "megaman", "football", "alakazam"]
random = words[rand(words.length+1)]
random_mod = random.dup
pattern = random.length
puts "The word is #{pattern} letters long"
x = random.gsub(/[a-z]/, '-') 
puts x
if
  pattern <= 6
  puts "You have only 10 tries"
  counter = 0
  loop do
    puts "Type a letter"
    input = gets.chomp
    counter += 1    
      if 
        random_mod.include? "#{input}"
        y = random_mod.index("#{input}")
        random_mod[y] = " "
        x[y] = "#{input}"
         puts x
 	    break if counter == 10 || x == random 
 	else
 	    puts "The suggested letter is not present in the word"	
 	    break if counter == 10 || x == random 
 	end
  end
else
  puts "You have only 14 tries"
  counter = 0
  loop do
    puts "Type a letter"
    input = gets.chomp
    counter += 1    
     if 
      random_mod.include? "#{input}"
      y = random_mod.index("#{input}")
      random_mod[y] = " "
      x[y] = "#{input}"
 	  puts x
 	  break if counter == 14 || x == random 
 	else
 	  puts "The suggested letter is not present in the word"	
 	  break if counter == 14 || x == random 
 	end
  end
end
puts "Congrats! You won!" if x == random
puts "Sorry, you lost. Try next time" if !(x == random)
 

MegamanEXE

Well-known member
Dec 7, 2007
1,958
1
43
28
Islamabad
All right i have done the hangman... although it gives a weird error if i enter space instead of any letter and if i enter two letters at once? @MegamanEXE does yours give any errors if you do this?
If you have time.... can you test this code and give me some feedback.. here's an online compiler
repl.it

Here is the code
Spoiler: show
Code:
words = ["glove", "emblem", "dragon", "hell", "pakistan", "rexcalibur", "london", "megaman", "football", "alakazam"]
random = words[rand(words.length+1)]
random_mod = random.dup
pattern = random.length
puts "The word is #{pattern} letters long"
x = random.gsub(/[a-z]/, '-') 
puts x
if
  pattern <= 6
  puts "You have only 10 tries"
  counter = 0
  loop do
    puts "Type a letter"
    input = gets.chomp
    counter += 1    
      if 
        random_mod.include? "#{input}"
        y = random_mod.index("#{input}")
        random_mod[y] = " "
        x[y] = "#{input}"
         puts x
         break if counter == 10 || x == random 
     else
         puts "The suggested letter is not present in the word"    
         break if counter == 10 || x == random 
     end
  end
else
  puts "You have only 14 tries"
  counter = 0
  loop do
    puts "Type a letter"
    input = gets.chomp
    counter += 1    
     if 
      random_mod.include? "#{input}"
      y = random_mod.index("#{input}")
      random_mod[y] = " "
      x[y] = "#{input}"
       puts x
       break if counter == 14 || x == random 
     else
       puts "The suggested letter is not present in the word"    
       break if counter == 14 || x == random 
     end
  end
end
puts "Congrats! You won!" if x == random
puts "Sorry, you lost. Try next time" if !(x == random)

This is, indeed, very nice! Well done! The space problem is probably related to how Ruby handles strings (which is already weird enough as far as I've read in that link I sent you). But yes, this is very great for a beginner, pat yourself on the back. ^_^

EDIT: Stuff removed, this is enough :)
 
Last edited:

r3v3rs3

Proficient
May 25, 2007
586
1
23
a=a^b;
b=a^b;
a=a^b;
It took me a while to figure out what you were trying to say here. The symbol "^" is usually:
1) used for exponent.
2) or for the "and" function in context of binary variables.
I am assuming you used it for exclusive-or / xor.

Here's a verification of this method:
to avoid confusion lets use the letters A and B for the initial "binary" values before the execution of these commands.
1) a = a xor b
after this command:
a = A xor B
b = B

2) b = a xor b
after this command:
a = A xor B
b = (A xor B) xor B = A xor (B xor B) = A xor 0 = A

3) a = a xor b
after this command:
a = (A xor B) xor A = A xor (B xor A) = A xor (A xor B) = (A xor A) xor B = 0 xor B = B
b = A

So this method does work.
But I am interested in seeing what would be a good way for 'arriving' at this method. Like, for example, several simple math equalities ( say (n+1)^2 = n^2 + 2n + 1 ) you can 'prove' by induction. But usually arriving at those equalities is easier through some other insight (like simple algebraic multiplication in my example).

Secondly can this exchange be done through other binary functions too (and which ones)?

---

At any rate, I gave it some more thought. For variables supposed to represent rational and real nos. I think the following method should work:
for the case b>a do the following
1) a = a + (b-a)/2
2) b = a - (b-a)
3) a = a + (a-b)
Lets assume the initial "numeric" values for variables "a" and "b" to be "A" and "B" before the execution of these commands.
1) a = a + (b-a)/2
after this command:
a = A + (B-A)/2
b = B
2) b = a - (b-a)
after this command:
a = A + (B-A)/2
b = (A+(B-A)/2) - (B - (A+(B-A)/2)) = 2(A+(B-A)/2) - B = A
3) a = a + (a-b)
after this command:
a = (A+(B-A)/2) + ((A+(B-A)/2) - A) = 2(A+(B-A)/2) - A = B
b = A

for the case a>b we add another if-condition and reverse the expressions of course.

For variables supposed to represent integers we would have to do something different. I think this should work:
for the case b>a do the following
1) a = a + 2*(b-a)
2) b = b - (a-b)
3) a = a - (a-b)/2

Lets assume the initial "numeric" values for variables "a" and "b" to be "A" and "B" before the execution of these commands.
1) a = a + 2*(b-a)
after this command:
a = A + 2*(B-A) = 2*B - A
b = B
2) b = b - (a-b)
after this command:
a = 2*B - A
b = B - ((2*B-A) - B) = B - (B-A) = A
3) a = a - (a-b)/2
after this command:
a = (2*B-A) - ((2*B-A) - A)/2 = (2*B-A) - (2*B-2*A)/2 = (2*B-A) - (B-A) = B
b = A

for the case a>b we add another if-condition and reverse the expressions.

---

Now ofc two things to note are:
1) all the expression verification I did isn't really required for the methods I described(seems more suitable in the binary function case you described). Shouldn't be too difficult to see intuitively, but I can't sleep well at night without writing such clumsy algebraic expressions.
2) The second method I described would also work for variables representing rational or real numbers ofc. So it could be considered general in that sense.
 
Last edited:

MegamanEXE

Well-known member
Dec 7, 2007
1,958
1
43
28
Islamabad
@r3v3rs3
Fan of Maths? :p

Wow, no, I'm not thinking it like THAT! o_O (Though I wish I could). I'm just thinking it in term of bits. Since everything in computers whether they're numbers or alphabets, they're represented as bits. For example if A is 1101 (representing something) and B is 0101. Doing XOR (if bits are different, resulting bit is 1 otherwise 0)

1101 A
0101 B
------
1000 (new A)
0101 B
------
1101 (New & Final B)
1001 (New A)
------
0101 (Final A)

So finally, we get: A=0101 and B=1101

i.e. the bits have been exchanged and now represent what they're supposed to represent.

The thinking goes along the lines of "If you complement something, then complement it again, you'll get the same thing back" (Double NOTs). The point is, you can get something back even by doing operations on it. XOR is interesting in this respect.

Well, you know, operations have their reverses (or r3v3rs3 if you like :p Bad pun). Addition cancels subtraction, Division cancels multiplication. So you can also swap using these operations. For example if a=2 and b=3:
a = a*b; (a=6)
b= a/b (6/3=2) [b is now 2, exactly what we want, now to get back a]
a= a/b (6/2=3)

A is now 3, B is now 2. You can do the same thing with addition/subtraction combo.
 
Last edited:

EternalBlizzard

Lazy guy :s
Moderator
Oct 29, 2011
2,732
1,193
129
Attractor Field Beta
a = a*b; (a=6)
b= a/b (6/3=2) [b is now 2, exactly what we want, now to get back a]
a= a/b (6/2=3)

A is now 3, B is now 2. You can do the same thing with addition/subtraction combo.
I actually managed to do that like this method but i thought that this won't work if values are strings...... Your method will work won't it...?
 

Shary Bhallu TC

Bhallu is dead, legacy remains
Jun 2, 2009
16,491
692
129
30
Karachi.
Dammit I am totally stuck in one of the questions of my assignment...

Its one of those currency denominations questions, but its quite different in this question we have to input the amount we have to pay, amount of 100 rupee, 50 rupee and 20 rupee notes we have, and then find out how many of those notes are required to make the amount we have to pay.

Now I've made a program, but the problem is that even when I input 0 in the amount of 100 rupee notes I have, I still get some value of 100 rupee notes required to form the amount we have to pay.

How do I get across this? Help this noob pls! (btw we cant use strings)
 

Newton

Well-known member
May 17, 2009
2,223
0
41
Lahore, Faisalabad
Dammit I am totally stuck in one of the questions of my assignment...

Its one of those currency denominations questions, but its quite different in this question we have to input the amount we have to pay, amount of 100 rupee, 50 rupee and 20 rupee notes we have, and then find out how many of those notes are required to make the amount we have to pay.

Now I've made a program, but the problem is that even when I input 0 in the amount of 100 rupee notes I have, I still get some value of 100 rupee notes required to form the amount we have to pay.

How do I get across this? Help this noob pls! (btw we cant use strings)
post the code here and so that the mistake can be found :D
 

rafay22

. . :::::TEH UNBOXER::::: . .
Feb 4, 2011
1,622
0
42
28
Islamabad
Anybody here who has done the GLUT thing, aka OpenGL/Computer Graphics with C++ and Visual Studio.....?
 

MegamanEXE

Well-known member
Dec 7, 2007
1,958
1
43
28
Islamabad
Dammit I am totally stuck in one of the questions of my assignment...

Its one of those currency denominations questions, but its quite different in this question we have to input the amount we have to pay, amount of 100 rupee, 50 rupee and 20 rupee notes we have, and then find out how many of those notes are required to make the amount we have to pay.

Now I've made a program, but the problem is that even when I input 0 in the amount of 100 rupee notes I have, I still get some value of 100 rupee notes required to form the amount we have to pay.

How do I get across this? Help this noob pls! (btw we cant use strings)
Although I'm not entirely sure about your question, but for this stuff, you have to do 'defensive programming', meaning you have to consider cases like 0 and negative numbers, If it's 0 or less, throw an error message. something like if(amount<=0) ERROR else { the rest of your working code }
 
General chit-chat
Help Users
We have disabled traderscore and are working on a fix. There was a bug with the plugin | Click for Discord
  • No one is chatting at the moment.
    Necrokiller Necrokiller: Is it just me or people lately seem to defend every bad game design decision made by the devs...
    • Haha
    Reactions: EternalBlizzard