Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - Munchor

Pages: 1 ... 28 29 [30] 31 32 ... 424
436
Math and Science / Re: Loop all possible words algorithm
« on: August 05, 2011, 11:25:20 am »
ephan, I'll give you a tip: Lua starts with 1, python with 0 :)
(/me waits for the beating)

@Levak, lol

Jim, I did not understand your algorithm, I just copied so I don't know where it is wrong.

Code: [Select]
def increment(x):
    """Increments the string x"""
    length_of_string = len(x)
    if length_of_string == 0:
        return "a"
    bt = ord(x) #The problem is that ord doesn't work for strings, only characters
    if bt < 122:
        x = x[-1] + chr(bt+1)
    else:
        x = increment(x[-1] + "a")
    return x
   
def main():
    """Displays all possible words using alphabet a-z"""
    a = "a"
   
    for i in range(1000):
        a = increment(a)
        print a
 
if __name__ == "__main__":
    main()

So I tried:

Code: [Select]
def increment(x):
    """Increments the string x"""
    length_of_string = len(x)
    if length_of_string == 0:
        return "a"
    bt = x.encode("hex")
    if bt < 122:
        x = x + chr(bt+1)
    else:
        x = increment(x + "a")
    return x
   
def main():
    """Displays all possible words using alphabet a-z"""
    a = "a"
   
    for i in range(1000):
        a = increment(a)
        print a
 
if __name__ == "__main__":
    main()

But I get recursion errors.

437
Computer Programming / Re: char[8] to char[8] giving error
« on: August 05, 2011, 11:15:58 am »
By making the char array able to hold 9 chars to make the string 0 terminated.

Nicely spotted. The strcmp( is not working, when we tie, it says "You lose" :/ Current code:

Code: [Select]
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>

int main();

int main() {
  char options[][9] = {"rocks", "paper", "scissors"}; //Possible options
  srand( time(NULL) ); //For random integer
 
  /* Define variables needed */
  int user_choice_index;
  char user_choice[9];
  int cpu_choice_index;
  char cpu_choice[9];
  int i;
 
  while (1) {
    /* Get user choice and index */
    fgets(user_choice, sizeof user_choice, stdin);
    for (i=0; i<3; i++)
    {
      if ( user_choice == options[i] )
      {
        user_choice_index = i; //Index of user's choice
        break;
      }
    }
   
    /* Get computer choice */
    cpu_choice_index = ( rand() %3 ); //Random number [0,3]
    strcpy(cpu_choice, options[cpu_choice_index]);
    printf("%s\n", cpu_choice);
   
    /* Check who wins */
    if ( strcmp(cpu_choice, user_choice) == 0 )
    {
      printf("Tie\n");
      continue;
    }
    else if ( (cpu_choice_index + 1) % 3 == user_choice_index )
    {
      printf("You won\n");
      continue;
    }
    else
    {
      printf("You lose\n");
      continue;
    }
  }
 
  return 0;
}

438
Math and Science / Re: Loop all possible words algorithm
« on: August 05, 2011, 10:52:30 am »
Code: [Select]
def increment(x):
    """Increments the string x"""
    length_of_string = len(x)
    if length_of_string == 0:
        return "a"
    bt = ord(x)
    if bt < 122:
        x = x[1:-1] + chr(bt+1)
    else:
        x = increment(x[1:-1] + "a")
    return x
   
def main():
    """Displays all possible words using alphabet a-z"""
    a = "a"
   
    for i in range(1000):
        a = increment(a)
        print a
 
if __name__ == "__main__":
    main()

Heh Jim, I tried to copy cat your version to Python, but failed :P

439
Computer Programming / Re: char[8] to char[8] giving error
« on: August 05, 2011, 10:43:05 am »
Change
Code: [Select]
cpu_choice = options[cpu_choice_index];

to

Code: [Select]
*cpu_choice = options[cpu_choice_index];

Could you see my edit please? :) And that doesn't work by the way HOMER-16, since you're making an integer from a pointer without a cast.

440
Computer Programming / char[8] to char[8] giving error
« on: August 05, 2011, 09:53:04 am »
Inspired on the Rock, Papers, Scissors topic I decided to try and make a C version:

Code: [Select]
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>

int main();

int main() {
  char options[][8] = {"rocks", "papers", "scissors"}; //Possible options
  srand( time(NULL) ); //For random integer
 
  /* Define variables needed */
  int user_choice_index;
  char user_choice[8];
  int cpu_choice_index;
  char cpu_choice[8];
  int i;
 
  while (1) {
    /* Get user choice and index */
    fgets(user_choice, sizeof user_choice, stdin);
    for (i=0; i<3; i++)
    {
      if ( user_choice == options[i] )
      {
        user_choice_index = i; //Index of user's choice
        break;
      }
    }
   
    /* Get computer choice */
    cpu_choice_index = ( rand() %3 ); //Random number [0,3]
    cpu_choice = options[cpu_choice_index];
   
    /* Check who wins */
    if ( strcmp(cpu_choice, user_choice) == 0 )
    {
      printf("Tie\n");
    }
    else if ( (cpu_choice_index + 1) % 3 == user_choice_index )
    {
      printf("You won");
    }
    else
    {
      printf("You lose");
    }
  }
 
  return 0;
}

However, when compiling I get:

Quote
rock_papers_scissors.c:33:16: error: incompatible types when assigning to type ‘char[8]’ from type ‘char *’

On the line:

Code: [Select]
cpu_choice = options[cpu_choice_index];
Each member of the array options is of type char[8], as you can see in the declaration:

Code: [Select]
char options[][8] = {"rocks", "papers", "scissors"}; //Possible options
So why does it think it is a char * instead of a char[8]? Thanks!

EDIT

I recorded I was copying addresses and used strcpy(); but the program isn't work that well:

Code: [Select]
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>

int main();

int main() {
  char options[][8] = {"rocks", "paper", "scissors"}; //Possible options
  srand( time(NULL) ); //For random integer
 
  /* Define variables needed */
  int user_choice_index;
  char user_choice[8];
  int cpu_choice_index;
  char cpu_choice[8];
  int i;
 
  while (1) {
    /* Get user choice and index */
    fgets(user_choice, sizeof user_choice, stdin);
    for (i=0; i<3; i++)
    {
      if ( user_choice == options[i] )
      {
        user_choice_index = i; //Index of user's choice
        break;
      }
    }
   
    /* Get computer choice */
    cpu_choice_index = ( rand() %3 ); //Random number [0,3]
    strcpy(cpu_choice, options[cpu_choice_index]);
    printf("%s", cpu_choice);
   
    /* Check who wins */
    if ( strcmp(cpu_choice, user_choice) == 0 )
    {
      printf("Tie\n");
    }
    else if ( (cpu_choice_index + 1) % 3 == user_choice_index )
    {
      printf("You won\n");
    }
    else
    {
      printf("You lose\n");
    }
  }
 
  return 0;
}

When I inputed scissors, here's what happened:

Quote
scissors
scissors�@You won
rocksYou lose

441
Computer Programming / Re: Rock Paper Scissors
« on: August 05, 2011, 09:13:04 am »
Michael Lee, I suggest using random.choice() to choose randomly from a list. With calc84maniac's implementation this can't be done though because the bot could also choose "list".

442
Humour and Jokes / Re: Drink something.
« on: August 05, 2011, 07:41:44 am »
Poor chemist, too bad the joke is English-only.

443
News / Re: Some changes on Facebook
« on: August 05, 2011, 07:16:30 am »
This is cool, thus I think more news appear and more people will "Like" us ;)

444
Quote
Seriously, though, I have no idea where else to put it. And I want to put it somewhere

My problem is using it, having validated pages is cool, but showing it off is not so cool in my opinion. The only important website using it was python.org, but they removed it recently.

445
Looking awesome Deep Thought, I dislike the W3C Compliant button, it just looks bad :P

446
TI Z80 / Re: Croquette IDE
« on: August 04, 2011, 03:47:54 pm »
Some progress today:

  • Run File Option
  • Code Improvement

The "Run File" option uses "wxwabbitemu" as default, but will soon be customizable, so that the user can also use "wabbitemu" for example :)

I'll also be adding custom fonts soon.

447
Quote
Nspire-Lua environment for Gedit (Programming Editor for Ubuntu)

Also, Gedit is not Ubuntu-Only, it's "Gnome-Editor". I'm just installing it by the way.

448
Other / Re: What computer OS do you use?
« on: August 04, 2011, 01:44:24 pm »
On Linux, can I save things to the Windows partition, then?

yes you can. if you want to share files, though, you can use the cloud, I think you have a server, don't you? :)

449
Calculator C / Re: C Q&A Thread
« on: August 04, 2011, 12:36:17 pm »
math.h is a default library, so it should be included with <>, #include <math.h>.

Oh wait, are you creating your own math.h? I think you should give it a new name, so it doesn't conflit with the default.

450
Other / Re: What computer OS do you use?
« on: August 04, 2011, 12:30:00 pm »
Quote
Hrm, random question (before I forget) -- if I dual boot both a Linux OS and a Windows OS on the same computer, is there a way to set it up in a way that they can both share each others' files (like my papers for school and stuff, and code I'm writing).

I can easily access my Windows files from Linux, not sure if otherwise, never had to.

Pages: 1 ... 28 29 [30] 31 32 ... 424