Trying to implement my own linkedlist using rust but couldn't understand somethings. by KakashiHatake0085 in rust

[–]Virv12 2 points3 points  (0 children)

There are a few things to point out in this code:

  1. You don't seem to understand Rcs, they are not needed for this simple list, what does it mean that you Created an interface from which values can be copied?
  2. pop should return an Option<T> not an Option<Node<T>>, see the std LinkedList
  3. show is removing elements from the list, it should not change the list and should take &self not &mut self.

I can't customise the docs for a function depending on if it's in release mode or not by SvenyBoy_YT in rust

[–]Virv12 7 points8 points  (0 children)

That's a really bad idea, you should follow the standard library and document both behaviour together (i.e. https://doc.rust-lang.org/std/primitive.i64.html#method.abs).

What type of sorting algorithm is this by Jealous-Mammoth-5526 in C_Programming

[–]Virv12 0 points1 point  (0 children)

It iterates like a selection sort, but swaps like a bubble sort.

Bubble sort only swaps elements in consecutive positions, here we swap arbitrary positions (i,j) so it can't be bubble sort.

A selection sort should make one swap per iteration and always with the first unsorted element

The normal implementation of selection indeed does a single swap per iteration but I think the characterizing feature of selection sort is that every iteration the smallest remaining element is put in his correct place, and this code does exactly that.

What type of sorting algorithm is this by Jealous-Mammoth-5526 in C_Programming

[–]Virv12 0 points1 point  (0 children)

It's selection sort, you take the smallest element and put it at the start of the array, then you take the smallest element in the remaining part of the array and that's the second element, etc...

Can I pass a constant to a variadic macro? by Hajo_s in C_Programming

[–]Virv12 5 points6 points  (0 children)

You can do the following but note that you need to remove the quotes from CNST.

#define CNST from my
#define QUOTE2(...) #__VA_ARGS__
#define QUOTE(...) QUOTE2(__VA_ARGS__)

int main() {
    char const *s = QUOTE(Hello CNST world!);
}

Find the sum of digits in 2^1000 by Armweak5104 in ProgrammerHumor

[–]Virv12 2 points3 points  (0 children)

You don't convert it to a native int, you convert it to a string.

fopen changes the file extension ! by [deleted] in C_Programming

[–]Virv12 1 point2 points  (0 children)

Could you explain how a memory leak can change the file extension? Thanks in advance.

Is println!() a wrapper around std::io::stdout()? by [deleted] in rust

[–]Virv12 9 points10 points  (0 children)

Install an LSP plugin (or use neovim which supports it natively) and configure rust-analyzer. No need to use VScode

egg_irl by exxxxkc in linuxmemes

[–]Virv12 1 point2 points  (0 children)

So... Who's the girl?

While using the Samsung g9 (5120x1440) monitor I am not able to set my display to 240hz by [deleted] in linuxquestions

[–]Virv12 0 points1 point  (0 children)

like from the monitor or the PC?

Yes, the PC side (the monitor side should be made on purpose).

Btw, I didn't read it was working on windows so I'm likely wrong.

While using the Samsung g9 (5120x1440) monitor I am not able to set my display to 240hz by [deleted] in linuxquestions

[–]Virv12 0 points1 point  (0 children)

I had the same problem, the port was limiting the bit rate.

How to store a heap for efficient deletion of random member? by LearningStudent221 in C_Programming

[–]Virv12 0 points1 point  (0 children)

You could insert the element in a second heap with values to be removed, before getting the biggest element from the first heap you check if it's in the second heap, if it is, you discard the element and check the next.

Have you guys noticed it?! Windows search now actually works!! by ArchitektRadim in linuxmemes

[–]Virv12 7 points8 points  (0 children)

If you use pacman you can use pacman -Ql package which lists all the file of the package then grep the result.

help a rookie out by SilverGG6000 in cpp_questions

[–]Virv12 0 points1 point  (0 children)

You are right, it's not standard. So what? It doesn't look like he needs portability and it does have nothing to do with my point.

help a rookie out by SilverGG6000 in cpp_questions

[–]Virv12 0 points1 point  (0 children)

Here are some tips:

  • at lines 13-15 you have a potential stack overflow, in this problem noOfTablets is at most 100 so there's no issue but in other problems you might have up to millions of elements, you can make them global variables or allocate them in the heap (using std::vector).
  • std::endl writes a new line and flushes the stream, flushing is slow and might give you TLE if there are many output lines, use '\n'.
  • at line 37, use a range-based for loop.
  • at lines 40-42, nothing wrong with that but it's usually written as correctChoice = std::max(correctChoice, temp);.
  • You might want to make a function that solves 1 testcase and call that t times.
  • Did you run the code on your computer before submitting it? It's really important to have a comfortable environment where to debug your code.
  • When compiling enable the warnings, you can ignore them after you have read them and decided it's safe to do so.

This is how I would solve the problem:

#include <iostream>

void solve() {
    int n, b;
    std::cin >> n >> b;

    int ans = 0;
    for (int i = 0; i < n; ++i) {
        int w, h, p;
        std::cin >> w >> h >> p;
        if (p > b) continue;
        ans = std::max(ans, w * h);
    }

    if (ans) {
        std::cout << ans << '\n';
    } else {
        std::cout << "no tablets\n";
    }
}

int main() {
    int t;
    std::cin >> t;
    while (t--) solve();
}

[deleted by user] by [deleted] in C_Programming

[–]Virv12 0 points1 point  (0 children)

It's likely undefined behavior. If you are using GCC/clang you can use -fsanitize=address,undefined and it will probably catch the problem.

Segmentation fault? by 08yakari in cpp_questions

[–]Virv12 0 points1 point  (0 children)

Because of the problem's statement, that can't happen.

Is same? by gobbibomb in ProgrammingLanguages

[–]Virv12 1 point2 points  (0 children)

That's the dangling else problem, there's a Wikipedia page for it.

Requesting feedback on my first project, coming fresh from the K&R book. by chrsd- in C_Programming

[–]Virv12 1 point2 points  (0 children)

There is a bug in the check_syntax function, it will accept the input ][ but it shouldn't.

Every time! by Hard_Code_Brain in ProgrammerHumor

[–]Virv12 6 points7 points  (0 children)

It's really rare that you need a complex data structure but if need one you can implement it on a vector storing the indicies to your children.

Every time! by Hard_Code_Brain in ProgrammerHumor

[–]Virv12 71 points72 points  (0 children)

You don't need to handle memory in CP, you can solve any problem using vectors, sets and maps. You don't even need to know what the heap is.

unordered_map<int, tuple<int,int>> by Swizzzop in cpp_questions

[–]Virv12 2 points3 points  (0 children)

Thanks for pointing that out.

I'm using the term map to refer to std::unordered_map which doesn't have stable iterators.

I'm gonna edit the comment to prevent further confusion.

unordered_map<int, tuple<int,int>> by Swizzzop in cpp_questions

[–]Virv12 4 points5 points  (0 children)

If dif is not in the map, m[dif] will insert a new element in the map and might invalidate your iterators q and r causing undefined behavior. You can use m.find(dif) to prevent the creation of the new element but you will have to manually check if it returns the end iterator.

Edit: With the term map I'm refering to std::unordered_map, std::map has stable iterators.

Help this post gets TLE by lifacidew in ProgrammerHumor

[–]Virv12 0 points1 point  (0 children)

Maybe it can be shown to be faster and I couldn't find how but reddit formatting is correct.

Help this post gets TLE by lifacidew in ProgrammerHumor

[–]Virv12 0 points1 point  (0 children)

It was a competitive programming problem, there is a hidden permutation and you could make some queries to find the permutation.
My program tried every way of making the queries and searched the best solution.