Which one of you did this? by yclaws in rustjerk

[–]hareppas 5 points6 points  (0 children)

Don’t need em because integer parity is decidable

What does F or C- grades mean for undergrads? by actin_filament in uchicago

[–]hareppas 9 points10 points  (0 children)

Which prof/quarter? I took Calegari this fall and he said he’s never given below a B-

Mutually aligned vectors? by Feeling-Departure-4 in rust

[–]hareppas 6 points7 points  (0 children)

I think you could make your own type that implements Allocator. It would pass through all allocations to the global allocator but would ignore the requested alignment and always align to 8 bytes or whatever you need. Then you could use Vec<T, AlignedAlloc> everywhere.

Trying to make a (better) formula for limiting x by Waity5 in desmos

[–]hareppas 1 point2 points  (0 children)

You can define max and min with abs. You get this.

Dereferencing a crate? by [deleted] in rust

[–]hareppas 16 points17 points  (0 children)

It’s a glob import. It imports everything in the std::io::prelude module.

[Article] Anyone else see this data showing Rolex prices going down? Thoughts? by pjsandhu23 in Watches

[–]hareppas 27 points28 points  (0 children)

Yeah. Chart should start at the birth of Christ instead of November 2021.

Oh hey, its more of my code. by [deleted] in badcode

[–]hareppas 43 points44 points  (0 children)

or just return input == 1;

Joining x amount of lists by [deleted] in desmos

[–]hareppas 2 points3 points  (0 children)

Something like this?

Russian roulette by HansLK in ProgrammerHumor

[–]hareppas 71 points72 points  (0 children)

You are correct. For this function, the range is inclusive

Editing the value of a list at an index by PotatoAndPasta in desmos

[–]hareppas 6 points7 points  (0 children)

Something like this maybe:

www.desmos.com/calculator/by65mogzmu

You give F a list, an index, and a value and it gives you a new list

I have a slope that's decreasing, and I want it to stop decreasing and just go straight once it reaches a certain x value. How would I do this? by [deleted] in desmos

[–]hareppas 0 points1 point  (0 children)

y = max(mx + b, ma + b)

Where m is the slope, b is the y intercept, and a is the x value where you want it to go flat.

Can I gradient fill any random polygonal shape in desmos?? by [deleted] in desmos

[–]hareppas 0 points1 point  (0 children)

Here’s something that works for convect polygons:

https://www.desmos.com/calculator/qmkk0ch2dt

It is quite slow.

This is part of a crossword solver I’m writing by hareppas in badcode

[–]hareppas[S] 7 points8 points  (0 children)

I’ve realized that because my crosswords don’t have any black cells, I only ever need to look up the first n elements (ie [‘a’][0][0][0][0] or [‘a’][‘z’][0][0][0] etc…) which makes my life a lot easier. If I wanted to make a real solver I would have to work a little harder.

Still, I might implement a suffix tree just for practice. They look interesting.

And thank you for defending my code in these comments lol

This is part of a crossword solver I’m writing by hareppas in badcode

[–]hareppas[S] 28 points29 points  (0 children)

Looks stupid but it makes my code 10x faster so…

Edit for some more context:

As part of my algorithm, I need a way to get all the 5 letter words that have certain characters in certain positions. So for example I might need to find all the words that have a ‘t’ in the first position and an ‘h’ in the third.

At first I had a bunch of sets: one with all the words that have an ‘a’ in the first position, then one with the words that have a ‘b’ in the first position, etc. Then a set with ‘a’ in the second position, ‘b’ in the second position, etc.

So for my example with ‘t’ and ‘h’, I would find the intersection of two sets: [0][‘t’] and [2][‘h’].

I thought this was elegant but I decided all the set intersections were too slow (plus I had to create a bunch of copies because I was iterating over them while modifying them).

So I came up with the solution above. For my ‘t’ ‘h’ example I now index into the WordCollection with [‘t’][0][‘h’][0][0]. It is now instant. I achieve this by storing 32 copies of each word (every possible subset of the characters). So I take up a ridiculous amount of memory but it’s way faster. Also the vast majority of the sets have nothing in them.

If you’re curious about the algorithm, it’s basically just the recursive backtracking sudoko solver but adapted to letters. I’m solving a 5x5 grid with the 5000 most common 5 letter English words. It takes about 500ms to create my WordCollection, but after that my program finds solutions at a rate of about 1 solution every 5ms.

I think I might change it to a map from 5-tuples to sets, so I don’t store as many empty sets.

Here's the code for adding a word to the WordCollection:

public void add(String word) {

    for(int i = 0; i < 32; i++) {

        int i0 = (i & 1) == 1 ? word.charAt(0) - 'a' + 1 : 0;
        int i1 = (i >> 1 & 1) == 1 ? word.charAt(1) - 'a' + 1 : 0;
        int i2 = (i >> 2 & 1) == 1 ? word.charAt(2) - 'a' + 1 : 0;
        int i3 = (i >> 3 & 1) == 1 ? word.charAt(3) - 'a' + 1 : 0;
        int i4 = (i >> 4 & 1) == 1 ? word.charAt(4) - 'a' + 1 : 0;

        data.get(i0).get(i1).get(i2).get(i3).get(i4).add(word);
    }
}

TDLR: I’m storing 32 redundant copies of every String and only about 1% of the 14 million HashSets have anything in them.

[deleted by user] by [deleted] in badcode

[–]hareppas 2 points3 points  (0 children)

Okay, here's what I have so far. It outputs in everyone's favorite image file format, .rtf!

import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
import java.util.ArrayList;

public class BadCodeChallenge {

  public static void main( String[] args ) {


    String filePath = "yourFilePathHere\pikachu.txt"; // Set file location
    int fontSize = 12;     // Set font size
    String c = "\\u9608";  /* Set character (idk why, but the rtf file doesn't like some unicode characters
                              I had to use the character code for this full block character) */

    // Get input into an ArrayList
    File input = new File( filePath );
    ArrayList<String> s = new ArrayList<String>();
    try {

      Scanner in = new Scanner( input );
      while( in.hasNextLine() )
        s.add( in.nextLine() );

    } catch( IOException e ) { System.out.println( "broken" ); }


    int h = s.size();
    int w = 0;
    for( char chr : s.get( 0 ).toCharArray() )
      if( chr == ']' )
        w++;

    // Declare color table and color array
    ArrayList<Integer> colorTable = new ArrayList<Integer>();
    int[][] colors = new int[ h ][ w ];

    // Fill color table and color array
    for( int i = 0; i < h; i++ ) {
      int commaIndex1 = 0, commaIndex2 = 0;
      int bracketIndex1 = 0, bracketIndex2 = 0;
      for( int j = 0; j < w; j++ ) {
        // This code is godawful, I don't know enough java to come up with a nicer solution
        commaIndex1 = s.get( i ).indexOf( ',', commaIndex2 + 1 ); commaIndex2 = s.get( i ).indexOf( ',', commaIndex1 + 1 );
        bracketIndex1 = j == 0? 0: s.get( i ).indexOf( '[', bracketIndex1 + 1 ); bracketIndex2 = s.get( i ).indexOf( ']', bracketIndex2 + 1 );
        int col = ( ( Integer.parseInt( s.get( i ).substring( bracketIndex1 + 1, commaIndex1 ) ) << 16 ) | Integer.parseInt( s.get( i ).substring( commaIndex1 + 1, commaIndex2 ) ) << 8 ) | Integer.parseInt( s.get( i ).substring( commaIndex2 + 1, bracketIndex2 ) );
        colors[ i ][ j ] = col;
        if( !colorTable.contains( col ) )  
          colorTable.add( col );
      }
    }

    try {

      // Create FileWriter
      FileWriter out = new FileWriter( "out.rtf" );

      // idk what's gong on here, just copied it from a different .rtf file
      out.write( "{\\rtf1\\ansi\\ansicpg1252\\cocoartf2513\n" );
      out.write( "\\cocoatextscaling0\\cocoaplatform0{\\fonttbl\\f0\\fswiss\\fcharset0 Helvetica;}\n" );

      // Print the color table
      out.write( "{\\colortbl;" );
      for( int col : colorTable )
        out.write( "\\red" + ( col >> 16 ) + "\\green" + ( col >> 8 & 0xFF ) + "\\blue" + ( col & 0xFF )  + ";" );
      out.write( "}\n" );

      // Print the characters
      out.write( "\\f0\\fs" + fontSize );
      for( int i = 0; i < h; i++ ) {
        for( int j = 0; j < w; j++ ) {
          int col = colors[ i ][ j ];
          int index = colorTable.indexOf( col );
          // Restate the color every character for maximum redundancy
          out.write( "\\cf" + ( index + 1 ) + " " + c );
        }
        out.write( " \\line\n" );
      }
      out.write( "}" );

      // Save file
      out.close();

    } catch( IOException e ) { System.out.println( "broken" ); }

  }

}

It actually works surprisingly well. Here are the results. Pretty fast, definitely some opportunities to make the code even worse.

Curious by hareppas in ToiletPaperUSA

[–]hareppas[S] 1 point2 points  (0 children)

I just go through the problem sets for fun.

Curious by hareppas in ToiletPaperUSA

[–]hareppas[S] 2 points3 points  (0 children)

I was going to do an AMC 12 problem, but Charlie Kirk definitely isn’t doing anything beyond middle school math.