Quasari

joined 1 year ago
[–] Quasari 71 points 1 year ago (1 children)

Numbers 5:11-31. Here's a Wikipedia Article about it.

[–] Quasari 1 points 1 year ago

I don't think your example is strong for the weak/strong link.

  1. It is their platform of what they want to do. Just because they say they will work towards a thing, doesn't mean that they are saying their opponents will not work for the opposite, instead it is saying if you vote for the others they will work in the opposite direction.
  2. Problems with the system(needing 60% to actually pass it) isn't a comment of the strength of the opponent, rather a weakness of the system.

I accept you want to think that the left is authoritarian, but for the most part that is not true in American politics.

[–] Quasari 2 points 1 year ago

functools.cache

That's pretty cool. I haven't dived too deep into python, so I should of looked up the library when you attached the @cache decorator. I learned something.

[–] Quasari 3 points 1 year ago (2 children)

Without memoization, I believe the Fibonacci sequence is O(2^N). It's dependent on how long a sequence of digits is in the input, so your worst case is like O(2^N) if the string is mostly digits and best case being O(N) if there are only 1 or 2 digit sequences.

Someone can correct me if I'm wrong.

[–] Quasari 2 points 1 year ago* (last edited 1 year ago)

So, I also realized its the fibonacci sequence for number of combinations of numbers. All I care about is counting sequential digits, we skip a character after every sequence. We just need to account for if the first character is a number and we are good. I did this in ruby. I didn't really try for anything bonus points wise.

Pastebin because formatting doesn't like & or <

My code also outputs 0 if the sequence itself is invalid(ends on a non-digit character, or has two non-digit characters in a row)

[–] Quasari 2 points 1 year ago* (last edited 1 year ago)

Ruby, just because I wanted a bit more practice. Again, I went to lowering character count, so it is ugly. I'm just using a anchor-runner pointer strategy to do the work. I tried to think of a regex to do the work for me, but couldn't think of one that would work.

i=ARGV[0]
o=''
a=r=0
l=i.length
while l>a
  r+=1
  if r>l||i[a]!=i[r] then
    o+=i[a]+(r-a).to_s
    a=r
  end
end
puts o
[–] Quasari 2 points 1 year ago* (last edited 1 year ago) (1 children)

I found this one easier than the medium as well. Maybe because I actually know a strategy for this one. Anywho, solution is in JavaScript. It's super ugly as I went for lowest character count, so no declarations, single char variable names, no semicolons, etc...

Basically use a anchor-runner pointer strategy to find a correct substring, I use a stack based solution for checking if the substring is correct. When the stack is empty, the substring is good, thus I record the start of it and its length. If, I get another good point, I just record the highest. If I hit a point where its no longer good, I jump the start to the end of the most recent good substring. Pretty fun.

Formatting screwed mine up, so heres a pastbin

[–] Quasari 3 points 1 year ago* (last edited 1 year ago)

You know, you are right. I overlooked the idea of there being multiple nests. That complicates things.

I could probably revise the current method, but build different n sized clusters through recursion, then just mix them.

Or, maybe just an insertion based one, placing a full bracket at every position in the string. That probably would be faster than the previous idea.

~~I guess I'll work on that tomorrow.~~

I ended up updating it now.

Thanks for the heads up.

[–] Quasari 3 points 1 year ago* (last edited 1 year ago) (2 children)

Older solution that doesn't work quite right

spoilerThis time I did it in JavaScript. Overall, it solves it in a reasonable amount of time up to n = 16, but won't at n = 18 and up.

const BRACKETS = [['(', ')'], ['[', ']'], ['{', '}']];

function brackets(n) {
  if (n === 1) return ['()', '[]', '{}'];
  let o = {};

  function addBracket(s) {
    BRACKETS.forEach(b => {
      o[b[0] + b[1] + s] = true;
      o[b[0] + s + b[1]] = true;
      o[s + b[0] + b[1]] = true;
    });
  }

  brackets(n - 1).forEach(addBracket);
  return Object.keys(o);
}

brackets(Number(process.argv[2]) / 2).forEach(v => console.log(v));

I don't feel experienced enough with data structures and algorithms to make this more efficient. I really just started learning this stuff and don't have a great grasp of it yet. I could of probably used a set to save some lines instead of a hashmap, but eh, its probably slightly faster because I went the hashmap route to get rid of duplicates.


I revised it because I was pointed out I missed an aspect of the problem. This is in JavaScript


const BRACKETS = ['()', '[]', '{}'];

function brackets(n) {
  if (n === 0) return [''];

  let strings = brackets(n - 1);

  let o = {};
  for (let s of strings) {
    for (let i = 0; brackets.length >= i; i++) {
      for (let b of BRACKETS) {
        o[s.slice(0, i) + b + s.slice(i)] = true;
      }
    }
  }

  return Object.keys(o);
}

brackets(Number(process.argv[2]) / 2).forEach(v => console.log(v));
[–] Quasari 1 points 1 year ago* (last edited 1 year ago) (3 children)

I have a concern. I don't think the tester can accurately test this problem. I could be wrong though

  1. Because test cases are per line(in that they are separated by \r\n), you can't necessarily have a multi-line solution, like most outputs will be \r\n for a print line function. If \n by itself is ok its easy enough, but...
  2. It checks strict equality with the solution, so the order your algorithm solves it in must be exactly the same as the test case. Its not impossible to do, but would probably require some q&a to solve exact order.
[–] Quasari 3 points 1 year ago* (last edited 1 year ago)

Here's an attempt in Ruby. I haven't coded in ruby in a while. I was going for character count, so I just used regex replacement.

o=i=gets.chomp
o=i.gsub!(/\(\)|{}|\[\]/,'') while o
puts i

I think I might do a speed one for fun.

Edit (command line input):

o=i=ARGV[0]
o=i.gsub!(/\(\)|{}|\[\]/,'') while o
puts i
[–] Quasari 4 points 1 year ago (1 children)

To be a superset, all elements of the subset must be contained within the superset.

TypeScript is a superset of JavaScript as valid JS code(just with everything implicitly the any type) is valid TS code.

view more: ‹ prev next ›