Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Locked thread
comedyblissoption
Mar 15, 2006

the correct type for main is () -> () :smugdog:

Adbot
ADBOT LOVES YOU

Sapozhnik
Jan 2, 2005

Nap Ghost
I mean I guess you could very carefully extend the bare minimum from the baseline of C in order to get the job done. Just templates and RAII and virtual methods, no exceptions and absolutely no loving operator overloading. Well, except you'll probably want a custom smart pointer class in order to do the RAII thing, so ok, maybe a tiny little bit of operator overloading.

No constructors, just assume that every new'ed object gets memset to 0 and make sure your destructors can handle that if your _init() methods fail, which is basically what you'd do in sensible C anyway. Well, except in C something is either uninitialized memory or a valid "object"; if your _init() function fails then it is the _init() function itself that is responsible for calling _fini() to undo the mess and return the object to being a mass of uninitialized junk that is no longer holding any resources. In this minimal C++ situation you'd instead have a pile of abortion waste on your stack that you have to be careful not to do anything with until it goes out of scope. eh.

constructors can't return an error code in c++ because that would break so much of the language, you have to throw an exception instead, and then, well you have to deal with all the myriad problems of exceptions in a non-garbage-collected language. this one aspect of c++ is a fount of aggravation and misery.

GCC has a nonstandard C extension instead that arranges for a cleanup method to be called whenever a stack variable (and only a stack variable) goes out of scope and it's a much nicer solution to this problem. the systemd guys use it extensively and it actually makes for rather nice code.

but there's still no nice way to do vtables in C. and you can't do just vtables in c++ without constructors and virtual destructors, and as described above you can't do those without exceptions.

c++. not even once.

Sapozhnik
Jan 2, 2005

Nap Ghost
http://250bpm.com/blog:4

comedyblissoption
Mar 15, 2006

rust doesn't have constructors or exceptions

it also yells at you if you forget about handling an error

gonadic io
Feb 16, 2011

>>=

comedyblissoption posted:

rust doesn't have constructors or exceptions

it also yells at you if you forget about handling an error

How are panics which you can catch not the same as exceptions?

hobbesmaster
Jan 28, 2008

Mr Dog posted:

constructors can't return an error code in c++ because that would break so much of the language, you have to throw an exception instead, and then, well you have to deal with all the myriad problems of exceptions in a non-garbage-collected language. this one aspect of c++ is a fount of aggravation and misery.

learn2raii noob
-C++ zealots

VikingofRock
Aug 24, 2008




gonadic io posted:

How are panics which you can catch not the same as exceptions?

That's sort of a philosophical question, but panics are intended to be used for logic errors and not normal flow control. So stuff like division by zero would panic, and stuff like failure to open a file would return the Err part of a Result. The rust community mostly sticks to this as well.

Actually, can you catch panics on stable now within the panicking thread? They used to be sort of like a thread-local abort.

fritz
Jul 26, 2003

hobbesmaster posted:

learn2raii noob
-C++ zealots

idk what you think the second 'i' in 'raii' does besides call a ctor

GameCube
Nov 21, 2006

NihilCredo posted:

if you really need those nested generics but don't want to type them everywhere, you can sometimes "alias" them depending on how you use them

code:

public interface WorbleGorble : IEnumerable<Tuple<IEnumerable<Mome>, IEnumerable<Chome>>>
{
}

agh gently caress i didn't realize I could make interfaces that inherited interfaces. poo poo

GameCube
Nov 21, 2006

AWWNAW posted:

or you can alias them using a using statement at the top of the file like using Dick = System.Tuple<Pussy,Taint>

shiiit

fritz
Jul 26, 2003

i like modern c++ and currentjob is absolutely dependent on it for cross platform stuff and libraries but it's still way too easy to gently caress up

hobbesmaster
Jan 28, 2008

fritz posted:

idk what you think the second 'i' in 'raii' does besides call a ctor

learn

2

raii

noob
-stroustrup

quote:

6 Handling of Constructors
An object is not considered constructed until its constructor has completed. Only then will stack
unwinding call the destructor for the object. An object composed of sub-objects is constructed to the extent
that its sub-objects have been constructed.
A well written constructor should ensure that its object is completely and correctly constructed. Failing
that, the constructor should restore the state of the system after failure to what it was before creation. It
would be ideal for naively written constructors always to achieve one of these alternatives and not leave
their objects in some ‘half-constructed’ state.
Consider a class X for which a constructor needs to acquire two resources x and y. This acquisition
might fail and throw an exception. Without imposing a burden of complexity on the programmer, the class
X constructor must never return having acquired resource x but not resource y
.
We can use the technique we use to handle local objects.

please ignore the words just before the bolded ones (page 12)

hobbesmaster
Jan 28, 2008

yes this is rather impractical

:thejoke:

comedyblissoption
Mar 15, 2006

gonadic io posted:

How are panics which you can catch not the same as exceptions?
i think you can only catch panics at thread boundaries and for FFI

or at least that's the intention and guideline if it's not being enforced somehow using types

at least the rust idioms avoid the criticisms of c++'s exception model listed in that article (I think)

edit: I guess you can catch panics wherever now:
https://doc.rust-lang.org/std/panic/fn.catch_unwind.html

i guess only time will tell if people abuse the hell out of this like exceptions

there's actually some limitations on what you can do when you catch a panic: https://doc.rust-lang.org/std/panic/trait.UnwindSafe.html

comedyblissoption fucked around with this message at 23:25 on Jul 23, 2016

MrMoo
Sep 14, 2000

I'm a terrible programmer, I cannot finish World CodeSprint 5 using Java 7. I keep getting timeouts on the palindromes, meh.

hobbesmaster
Jan 28, 2008

but can you with the same algorithm in a different language or is your algorithm just too slow?

like for example for the "easy" string construction one to not time out you need to have a set of characters you've seen

hobbesmaster
Jan 28, 2008

its probably isn't a hackerrank issue with the language but it could be

MrMoo
Sep 14, 2000

I guess it's my crap algo :shrug:
Java code:
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    public static void main(String[] args) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
        Scanner in = new Scanner(System.in);
        String s = in.next();
        int len = s.length();
        long count = 0;
        int a, b, c, d;
        for (a = 0; a < (len-3); a++) {
            final char ax = s.charAt(a);            
            for (b = a+1; b < (len-2); b++) {
                final char bx = s.charAt(b);                
                c = s.indexOf(bx, b+1);                
                for (; c < (len-1); c = s.indexOf (bx, c+1)) {                    
                    if (-1 == c)
                        break;
                    d = s.indexOf(ax, c+1);
                    for (; d < len; d = s.indexOf (ax, d+1)) {                        
                        if (-1 == d)
                            break;                            
                        count++;
                    }
                }
            }
        }
        System.out.println (count % 1000000007);
    }
}
:lol:

MrMoo fucked around with this message at 04:17 on Jul 24, 2016

GameCube
Nov 21, 2006

HoboMan posted:

wait, what? please elaborate so i don't have to learn the hard way as well

ok so here is the original code to generate the test cases. the IEnumerable<object[]> part is the stupid bullshit that XUnit requires for a Theory
code:
private static IEnumerable<IEnumerable<Lomarf>> PossibleChomes(IEnumerable<Lomarf>)
{
    ...
}

private static IEnumerable<IEnumerable<Mome>> GenerateMomes(IEnumerable<Lomarf> x,
                                                            IEnumerable<Lomarf> y,
                                                            IEnumerable<Lomarf> z)
{
    ...
}

public static IEnumerable<object[]> GenerateAllMomes(int nButts)
{
    // Create nButts unique lomarfs
    var lomarfs = Enumerable.Range(0, nButts)
                            .Select(x => LomarfFactory($"{x}"));

    // Calculate all possible chomes for nButts and assign these lomarfs to them
    var chomes = PossibleChomes(lomarfs);

    // Calculate all possible momes for each possible chome
    foreach (var chome in chomes)
    {
        foreach (var mome in GenerateMomes(lomarfs, lomarfs.Except(chome), chome))
        {
            yield return new object[] { mome };
        }
    }
}
with this code, the lomarfs passed to PossibleChomes() are not the same lomarfs passed to GenerateMomes(), because the iterator creates a new lomarf on each iteration. by adding ToList() after that Select() up there, we iterate through Enumerable.Range() immediately and lomarfs becomes a single List<Lomarf> that both PossibleChomes() and the foreach iterate over

GameCube
Nov 21, 2006

the chome/mome/lomarf thing is more annoying for you than it is for me but my employer is loving insane about IP

e: i meant more annoying for me. because i had to go through and replace all that poo poo so that nobody could crack my code. lomarf

GameCube fucked around with this message at 04:28 on Jul 24, 2016

hobbesmaster
Jan 28, 2008

MrMoo posted:

I guess it's my crap algo :shrug:
Java code:
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    public static void main(String[] args) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
        Scanner in = new Scanner(System.in);
        String s = in.next();
        int len = s.length();
        long count = 0;
        int a, b, c, d;
        for (a = 0; a < (len-3); a++) {
            final char ax = s.charAt(a);            
            for (b = a+1; b < (len-2); b++) {
                final char bx = s.charAt(b);                
                c = s.indexOf(bx, b+1);                
                for (; c < (len-1); c = s.indexOf (bx, c+1)) {                    
                    if (-1 == c)
                        break;
                    d = s.indexOf(ax, c+1);
                    for (; d < len; d = s.indexOf (ax, d+1)) {                        
                        if (-1 == d)
                            break;                            
                        count++;
                    }
                }
            }
        }
        System.out.println (count % 1000000007);
    }
}
:lol:



uhhhh O(n^4)?!

MrMoo
Sep 14, 2000

Jealous? It was worse. Highly dependent upon String.indexOf() performance. The inner two loops are more linear than the outer two.

MrMoo fucked around with this message at 04:41 on Jul 24, 2016

AWWNAW
Dec 30, 2008

i just looked at one of the sample problems and it made no sense, authored by a guy named ZeroCool

GameCube
Nov 21, 2006

tfw you start investigating ur test failures and discover that the test case generation algorithm u stayed up all night writing is producing invalid test cases :newlol:

GameCube
Nov 21, 2006

how do i add a local git hook that checks my commits for profanity before letting me commit? tia

GameCube
Nov 21, 2006

ok after fixing my test case algorithm my test passes for all 8,162 test cases :toot:

Bloody
Mar 3, 2013

MrMoo posted:

I guess it's my crap algo :shrug:
Java code:
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    public static void main(String[] args) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
        Scanner in = new Scanner(System.in);
        String s = in.next();
        int len = s.length();
        long count = 0;
        int a, b, c, d;
        for (a = 0; a < (len-3); a++) {
            final char ax = s.charAt(a);            
            for (b = a+1; b < (len-2); b++) {
                final char bx = s.charAt(b);                
                c = s.indexOf(bx, b+1);                
                for (; c < (len-1); c = s.indexOf (bx, c+1)) {                    
                    if (-1 == c)
                        break;
                    d = s.indexOf(ax, c+1);
                    for (; d < len; d = s.indexOf (ax, d+1)) {                        
                        if (-1 == d)
                            break;                            
                        count++;
                    }
                }
            }
        }
        System.out.println (count % 1000000007);
    }
}
:lol:



just peeped this

it is some nerd poo poo

hobbesmaster
Jan 28, 2008

there's a lot of good "learn _____ concept" things on hackerrank but the more "advanced" problems are usually simply poorly phrased and convoluted

even more so for the contests

Gul Banana
Nov 28, 2003

LordSaturn posted:

we need to stop expanding C++, it's 2016 and you can just use a good language if you loving want to

that is not c++. maybe this was the joke

MrMoo
Sep 14, 2000

hobbesmaster posted:

there's a lot of good "learn _____ concept" things on hackerrank but the more "advanced" problems are usually simply poorly phrased and convoluted

even more so for the contests

The Java advanced set include some great examples of how not to code, annotations is hosed up, and lambda expressions is clear the author has no idea what lambdas are for.

MrMoo fucked around with this message at 17:04 on Jul 24, 2016

GameCube
Nov 21, 2006

linq is so neat. i love linq. i want to marry linq

AWWNAW
Dec 30, 2008

same. if you don't like LINQ, gently caress you

LordSaturn
Aug 12, 2007

sadly unfunny

Gul Banana posted:

that is not c++. maybe this was the joke

no actually it just looked like standard issue C++ porpoise hork to me

GameCube
Nov 21, 2006

i've been doing nothing but c++ for like 6 years so all my c# looks like c++

Sapozhnik
Jan 2, 2005

Nap Ghost
the last time i had to professionally care about c++ was 2011 and i am very thankful for that

Bloody
Mar 3, 2013

GameCube posted:

linq is so neat. i love linq. i want to marry linq

Luigi Thirty
Apr 30, 2006

Emergency confection port.

GameCube posted:

linq is so neat. i love linq. i want to marry linq

epic thissery

Sapozhnik
Jan 2, 2005

Nap Ghost
java 8 streams are an acceptable substitute for linq

never want to go back to earlier versions of java

Gul Banana
Nov 28, 2003

linq is extremely good right up until you try to put the ?. operator in an Expression<Func<T,bool>> (e.g. the parameter to IQueryable.Where<T>)
then it gives you a compile error because adding support for newer language features to the AST would be backwards-incompatible :/

Adbot
ADBOT LOVES YOU

Notorious b.s.d.
Jan 25, 2003

by Reene

Mr Dog posted:

java 8 streams are an acceptable substitute for linq

never want to go back to earlier versions of java

java 8 adds proper collections libraries

java 9 is adding a repl

now all we need is for java 10 to get rid of the checked exceptions and java will finally be pleasant to work with

  • Locked thread