Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 542 543 [544] 545 546 ... 796

Author Topic: if self.isCoder(): post() #Programming Thread  (Read 882496 times)

TheBiggerFish

  • Bay Watcher
  • Somewhere around here.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8145 on: October 10, 2015, 09:19:50 pm »

Well, you have the sentinel declared outside of the loop...
But you never actually do anything to break out of the loop???
Your while is coded simply to check if starttime is less than 23.59.
What you need to do is
Code: [Select]
while(userChoice != 2){
//Move the error-checking statement INSIDE THE LOOP BODY.  Then proceed with processing.
//...Blah blah more code here.
}
//End of program stuff.
« Last Edit: October 10, 2015, 09:23:29 pm by TheBiggerFish »
Logged
Sigtext

It has been determined that Trump is an average unladen swallow travelling northbound at his maximum sustainable speed of -3 Obama-cubits per second in the middle of a class 3 hurricane.

3man75

  • Bay Watcher
  • I will fire this rocket
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8146 on: October 10, 2015, 09:27:01 pm »

Well, you have the sentinel declared outside of the loop...
But you never actually do anything to break out of the loop???
Your while is coded simply to check if starttime is less than 23.59.
What you need to do is
Code: [Select]
while(userChoice != 2){
//Move the error-checking statement INSIDE THE LOOP BODY.  Then proceed with processing.
//...Blah blah more code here.
}
//End of program stuff.


So what your saying is:

(userChoice != 2) {

while (startTime <23.59) { //This be the while loop i sorta have. Do I just click and move this here? Or Do I re-type it?

   code.code.code.}
}

While userChoice 1 would have something else to skip the while loop?
Logged

TheBiggerFish

  • Bay Watcher
  • Somewhere around here.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8147 on: October 10, 2015, 09:32:58 pm »

No, the while loop's condition should be userChoice != 2, it should not have anything to do with startTime.


In short, you put it in the wrong place, with the wrong condition, but you sort of have the right idea.


The while loop should go outside of any logic that actually deals with making sure the time is right, because you have to do that each time you run the loop (the input changes, you need to check it again).
And it should not be doing any checks on whether that user input is valid, that's what your if statements are for.
It should only be checking that the user doesn't want to end the program (thus why it's userChoice != 2.)
Logged
Sigtext

It has been determined that Trump is an average unladen swallow travelling northbound at his maximum sustainable speed of -3 Obama-cubits per second in the middle of a class 3 hurricane.

3man75

  • Bay Watcher
  • I will fire this rocket
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8148 on: October 10, 2015, 09:43:00 pm »

Spoiler (click to show/hide)

or like so?

Spoiler (click to show/hide)
Logged

TheBiggerFish

  • Bay Watcher
  • Somewhere around here.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8149 on: October 10, 2015, 09:59:52 pm »

Neither?
Here's a snippet of Java-y pseudocode because I don't know C++.  Any undeclared stuff is meant to stand in for your logic bits.
Code: [Select]
//...
int userChoice = 1;
//...
while(userChoice != 2){
//Get time input here.
if(input_isnt_valid){
//print error message
}
else{
//Do rate calculation
}
//take user continue input here
//if their input is 2, the loop will terminate
//if not it will run again.
}
//Goodbye user

//etc.
Logged
Sigtext

It has been determined that Trump is an average unladen swallow travelling northbound at his maximum sustainable speed of -3 Obama-cubits per second in the middle of a class 3 hurricane.

Moghjubar

  • Bay Watcher
  • Science gets you to space.
    • View Profile
    • Demon Legend
Re: if self.isCoder(): post() #Programming Thread
« Reply #8150 on: October 10, 2015, 11:39:12 pm »

Well, I finally got around to setting up git, sorta related: you can check out my only war character creator thingy I made here:
https://github.com/Moghjubar/OnlyWarCCreator/blob/master/main.cpp

Pretty much is all console/text and heavily uses selection and while loops, poke around with it if you want 3man75, uses only standard libs.
Logged
Steam ID
Making things in Unity
Current Project: Demon Legend
Also working on THIS! Farworld Pioneers
Mastodon

miauw62

  • Bay Watcher
  • Every time you get ahead / it's just another hit
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8151 on: October 11, 2015, 04:52:43 am »

protip: if your project is a single file you can just gist :v
doesn't really matter but its neater i guess?
Logged

Quote from: NW_Kohaku
they wouldn't be able to tell the difference between the raving confessions of a mass murdering cannibal from a recipe to bake a pie.
Knowing Belgium, everyone will vote for themselves out of mistrust for anyone else, and some kind of weird direct democracy coalition will need to be formed from 11 million or so individuals.

TheBiggerFish

  • Bay Watcher
  • Somewhere around here.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8152 on: October 11, 2015, 01:46:52 pm »

So I'm looking for a regex that will match anything that ISN'T [a-zA-Z] (s no numbers, no specials, no underscores, no nothing but English letters), at any point in a string, regardless of whether there's any alpha character in said string.
Here's my (relevant, I omitted output bits) code, which, frankly, might have its own problems, but getting the regex right (or at least confirmed to match a character that isn't a letter) is the problem that needs to be solved so I can find any other problems.
I tried The Google and StackExchange, but didn't find anything that actually works.



It's supposed to take a variable-length input, then find the strings that are a certain length out of that input.
Yes, I suppose there are better ways to implement this than Scanner...Hmh, in fact, I should just (learn and) do an EasyReader or whatever the heck it is...
Here's the code anyway!
Code: [Select]

        ArrayList<String> inputList = new ArrayList(0);
        Scanner console = new Scanner(System.in);
        StringBuilder input = new StringBuilder(0);
        boolean isInputValid = true;
        while(isInputValid){
            System.out.println("Please input a word.  Any characters that are not capital or lowercase letters will end the input period.");
            input.append(console.next());
            //If it doesn't match letters?  Something about negative lookaheads...
            if(!input.toString().matches("(?![a-zA-Z]$).*")){
                //if size is less than one...
                if(inputList.size() < 1){
                    System.out.println("Please input a word before terminating the input period.");
                }
                //This SHOULD trigger on
                else{
                    break;
                }
            }
            if(input.toString().matches("(?![a-zA-Z]$).*")){
                inputList.add(input.toString());
                input.setLength(0);
                System.out.println("Word added.");
            }
        }




E: Ooooor it could be that I forgot to negate that regexp?  Trying that.
E2:Nope, now it's just erroring all the time, which makes that a logic flaw.  Gleh.
E3:Oh, I need that in the other conditional, too.
E4:I just cannot figure this out for the life of me.  Uuugh.


E5: REGULAR EXPRESSIONS AAAAAAAAA


E6: What the flying skeletal carp is WRONG with this thing?!
This is broken too.
Length is a field.
Code: [Select]
        while(true){
            try{
                length = console.nextInt();
                if(length < 1){
                    throw new IllegalArgumentException();
                }
                break;
            }
            catch(IllegalArgumentException | InputMismatchException ex){
                System.out.println("Please input a number greater than zero and less than 2^31.");
            }
        }

Not to mention the bit where my code is apparently arbitrarily deciding WHEN to switch to the bit where it does this (following from the prior), it will immediately go into an infinite loop with the catch block.


I'm half-tempted to just throw the thing out and start from scratch.
Fixed that issue at least.  Though not the seemingly arbitrary switching.

E(IDK): What.  The.  FLYING SKELETAL CARP.
Code: [Select]

Please input a word.  Any characters that are not capital or lowercase letters will end the input period.
r
Please input a word before terminating the input period.
...
Code: [Select]
Please input a word.  Any characters that are not capital or lowercase letters will end the input period.
r
Word added.
...?!
Code: [Select]
Please input a word.  Any characters that are not capital or lowercase letters will end the input period.
r


r
Please input a number greater than zero and less than 2^31.
Please input a number greater than zero and less than 2^31.
Please input a number greater than zero and less than 2^31.
Please input a number greater than zero and less than 2^31.
Please input a number greater than zero and less than 2^31.
Please input a number greater than zero and less than 2^31.
Please input a number greater than zero and less than 2^31.
Please input a number greater than zero and less than 2^31.
Please input a number greater than zero and less than 2^31.
Please input a number greater than zero and less than 2^31.
Please input a number greater than zero and less than 2^31.
Please input a number greater than zero and less than 2^31.
Please input a number greater than zero and less than 2^31.
Please input a number greater than zero and less than 2^31.
Please input a number greater than zero and less than 2^31.
Please input a number greater than zero and less than 2^31.
* TheBiggerFish cancels Write Code: Too Insane.


E:Updated code.




E: Fixed one thing.  Second code block is now properly try-catching instead of looping through the catch block indefinitely.
Code: [Select]
        boolean didGetPositiveInteger = false;
        while(!didGetPositiveInteger){
            try{
                System.out.println("Please input the integer word length you would like to check.");
                length = Integer.parseInt(console.next());
                if(length < 1){
                    throw new IllegalArgumentException();
                }
                else didGetPositiveInteger = true;
            }
            catch(IllegalArgumentException ex){
                System.out.println("Please input a number greater than zero and less than 2^31.");
            }
        }
The input is still messed up though.


It will, no matter what I put in, run
1)The inner if() statement, AND the bit that adds the word.  Hopefully fixed by actually applying the "isn't a terminator" regex.
2)A normal loop through.
3)The portion where it asks for an integer length to check against.


Is there something in the logic in the first code block (at the top of this post) that would make it do that?


E(null): Yeah, I really am stumped.


Here's one last dump of the thing.


Code: [Select]

import java.util.ArrayList;
import java.util.Scanner;


public class StringLengthChecker {


static int length;


    public static void main(String[] args) {
        ArrayList<String> inputList = new ArrayList(0);
        Scanner console = new Scanner(System.in);
        StringBuilder input = new StringBuilder(0);
        boolean isInputValid = true;
        while(isInputValid){
            System.out.println("Please input a word.  Any characters that are not capital or lowercase letters will end the input period.");
            input.append(console.next());
            //If it doesn't match letters?  Something about negative lookaheads...
            if(input.toString().matches("(?![a-zA-Z]$).*")){
                //if size is less than one and it's not a valid word.
                if(inputList.size() < 1 && input.toString().matches("(?![a-zA-Z]$).*")){
                    System.out.println("Please input a word before terminating the input period.");
                }
                //This SHOULD trigger if it's false.
                else{
                    input.setLength(0);
                    break;
                }
            }
            if(input.toString().matches("(?![a-zA-Z]$).*")){
                inputList.add(input.toString());
                input.setLength(0);
                System.out.println("Word added.");
            }
        }
        boolean didGetPositiveInteger = false;
        while(!didGetPositiveInteger){
            try{
                System.out.println("Please input the integer word length you would like to check.");
                length = Integer.parseInt(console.next());
                if(length < 1){
                    throw new IllegalArgumentException();
                }
                else didGetPositiveInteger = true;
            }
            catch(IllegalArgumentException ex){
                System.out.println("Please input a number greater than zero and less than 2^31.");
            }
        }
        System.out.println("Out of the strings in this input");
        for(String pull : inputList){
            System.out.println(pull);
        }
        System.out.println("These strings were " + length + " character(s) long.");
        for(String output : sortInput(inputList)){
            System.out.println(output);
        }
    }
    public static ArrayList<String> sortInput(ArrayList<String> input){
        ArrayList<String> output = new ArrayList();
        for(String strToProcess : input){
            if(strToProcess.length() == length){
                output.add(strToProcess);
            }
        }
        return output;
    }   
}
I have officially thrown in the towel for now, I think.
« Last Edit: October 11, 2015, 04:28:37 pm by TheBiggerFish »
Logged
Sigtext

It has been determined that Trump is an average unladen swallow travelling northbound at his maximum sustainable speed of -3 Obama-cubits per second in the middle of a class 3 hurricane.

MagmaMcFry

  • Bay Watcher
  • [EXISTS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8153 on: October 11, 2015, 04:56:21 pm »

So I'm looking for a regex that will match anything that ISN'T [a-zA-Z] (s no numbers, no specials, no underscores, no nothing but English letters), at any point in a string, regardless of whether there's any alpha character in said string.
Here's my (relevant, I omitted output bits) code.
Code: [Select]

        ArrayList<String> inputList = new ArrayList(0);
        Scanner console = new Scanner(System.in);
        StringBuilder input = new StringBuilder(0);
        boolean isInputValid = true;
        while(isInputValid){
            System.out.println("Please input a word.  Any characters that are not capital or lowercase letters will end the input period.");
            input.append(console.next());
            //If it doesn't match letters?  Something about negative lookaheads...
            if(!input.toString().matches("(?![a-zA-Z]$).*")){
                //if size is less than one...
                if(inputList.size() < 1){
                    System.out.println("Please input a word before terminating the input period.");
                }
                //This SHOULD trigger on
                else{
                    break;
                }
            }
            if(input.toString().matches("(?![a-zA-Z]$).*")){
                inputList.add(input.toString());
                input.setLength(0);
                System.out.println("Word added.");
            }
        }

The regex you are currently using, "(?![a-zA-Z]$).*", matches any string except a string that has a single word character, then ends. Obviously that's not what you want.

Things you could do instead, that actually do what you need them to do:

Code: [Select]
if (input.toString().matches(".*[^a-zA-Z].*"))This matches any word that is made of anything except it has a non-alphabet character anywhere.

Code: [Select]
if (! input.toString().matches("[a-zA-Z]*"))This is way faster: It matches any word that is made entirely of alphabet characters. You want the opposite, so just put a not in front of the result.

Code: [Select]
if (Pattern.compile("[^a-zA-Z]").matcher(input.toString()).find())This is the best way to do it. Note that the find() method is not interested in matching the regex to the whole string, instead it's just interested in finding any substring that matches the expression. Use the match() method instead if you want to match only all or nothing. Also you can store the compiled pattern in a global variable instead of recompiling it every time to make it faster still. Like this:

Code: (Outside your main loop) [Select]
public static Pattern nonAlphabeticCharacterPattern = Pattern.compile("[^a-zA-Z]");
Code: [Select]
if (nonAlphabeticCharacterPattern.matcher(input.toString()).find())[/code]

Also you really really don't need the StringBuilder, just use a String instead. Here, I fixed up all your string input code, the rest is fine:

Code: [Select]
private static Pattern nonAlphabeticCharacterPattern = Pattern.compile("[^a-zA-Z]");

private static ArrayList<String> getWordsFromConsole() {
ArrayList<String> inputList = new ArrayList<String>();
Scanner console = new Scanner(System.in);
String input;
boolean isInputValid = true;
INPUTLOOP: while (isInputValid) {
System.out.println("Please input a word.  Any characters that are not capital or lowercase letters will end the input period.");
input = console.next();
if (nonAlphabeticCharacterPattern.matcher(input).find()) {
if (inputList.size() < 1) {
System.out.println("Please input a word before terminating the input period.");
} else {
break INPUTLOOP;
}
}
inputList.add(input);
System.out.println("Word added.");
}
console.close();
return inputList;
}

public static void main(String[] args) {
ArrayList<String> consoleWords = getWordsFromConsole();
// Here you go, do something with consoleWords
}
« Last Edit: October 11, 2015, 05:00:47 pm by MagmaMcFry »
Logged

TheBiggerFish

  • Bay Watcher
  • Somewhere around here.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8154 on: October 11, 2015, 05:37:27 pm »

PRAISE ARMOK, I AM SAVED!


Yeah, thanks.  I'll do that.


Can you do .matcher(input).find() with the pattern variable?


Yes you can.


Whew.  Thank you.  It's working.
« Last Edit: October 11, 2015, 06:16:13 pm by TheBiggerFish »
Logged
Sigtext

It has been determined that Trump is an average unladen swallow travelling northbound at his maximum sustainable speed of -3 Obama-cubits per second in the middle of a class 3 hurricane.

Avis-Mergulus

  • Bay Watcher
  • This adorable animal can't work.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8155 on: October 11, 2015, 06:27:15 pm »

So I was solving a bunch of short simple programming challenges for a class, and I came across a point where the automatic test battery I'm supposed to submit things to has failed two tests out of five in a place where there was literally almost nothing to fail. So the thing was, I had to write a function that would calculate the sum of two numbers without using loops or any arithmetic operation except incrementing or decrementing by one. So I did:
Code: [Select]
def sum(a, b):
    a += 1
    b -= 1
    if b != 0:
        return sum(a, b)
    else:
        return a
       
print(sum(int(input()),int(input())))

I know that it is tested on two positive integers. But it appears that at least some of the numbers in this test battery are large enough to cause a stack overflow, and I can't for the life of me think of another way to go about this. What am I missing?
Logged
“See this Payam!” cried the gods, “He deceives us! He cruelly abuses our lustful hearts!”

Antsan

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8156 on: October 11, 2015, 06:33:36 pm »

Try the case with b=0.
The other case may be with b<0.
Logged
Taste my Paci-Fist

MagmaMcFry

  • Bay Watcher
  • [EXISTS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8157 on: October 11, 2015, 06:48:09 pm »

Are you sure the given integers are necessarily positive?
Logged

Avis-Mergulus

  • Bay Watcher
  • This adorable animal can't work.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #8158 on: October 11, 2015, 07:17:36 pm »

Are you sure the given integers are necessarily positive?
At least, that's what the description says.

Try the case with b=0.
The other case may be with b<0.
Why would I do b=0? It'll just return a straight away and that'll be it. b<0 doesn't work either (why would it, if b is never less than zero on the first iteration?) Besides, my problem is not that I get incorrect answers; it's just that the program crashes. And I know for sure that it does crash on very large numbers.

I wonder if there's some way to shorten the stack, like introducing another argument to store transitional values?

E: Antsan, I completely missed your meaning. Yes, one of the cases was indeed b = 0, dunno how I missed that, sorry. There is still another case, though, which is not b < 0.
« Last Edit: October 11, 2015, 07:22:55 pm by Avis-Mergulus »
Logged
“See this Payam!” cried the gods, “He deceives us! He cruelly abuses our lustful hearts!”

Orange Wizard

  • Bay Watcher
  • mou ii yo
    • View Profile
    • S M U G
Re: if self.isCoder(): post() #Programming Thread
« Reply #8159 on: October 11, 2015, 07:34:50 pm »

Yeah that code won't work if b starts negative.

Unless I'm missing something, Avis' problem is how to keep summing positive integers without crashing because of recursion.
Logged
Please don't shitpost, it lowers the quality of discourse
Hard science is like a sword, and soft science is like fear. You can use both to equally powerful results, but even if your opponent disbelieve your stabs, they will still die.
Pages: 1 ... 542 543 [544] 545 546 ... 796