- #1
Jamin2112
- 986
- 12
Is it a good idea to do "coding drills" in C or C++98 just to make things harder? I've been doing that and it seems to help because there's so much more I need to think about in order to cover all the corner cases and make things compact. It's like practicing baseball with a weighted bat
so that swinging with a regular bat feels easier.
For instance, I just did
and, quite honestly, it took me like 10-15 minutes to do it because I have to think through all the pointer arithmetic and corner cases and try to make things compact (and it still isn't compact and probably is incorrect).
Maybe I have poor logical-mathematical ability, but I feel like it takes a lot of brain power just for me to stare at
(http://www.cplusplus.com/reference/algorithm/partition/) and visualize how everything is happening.
In any event, I love that sort of stuff because when I go to work everything seems easier by comparison. It's like taking the weight off the bat.
Is all this "code golf" stuff actually worth my time, though? I'm a Junior .NET developer and want to make good money some day. Should I be spending all my free time absorbing more .NET knowledge?
so that swinging with a regular bat feels easier.
For instance, I just did
Code:
#include <iostream>
bool contains ( int * sarr, size_t n, int i) // checks whether the integer i is in the sorted array sarr of length n
{
int * pa(sarr), * pb(sarr + n);
if (pa == pb) return false;
--pb;
while (pa != pb && ((pb - pa) != 1))
{
int * pc = pa + (pb - pa)/2;
if (*pc < i) pa = pc;
else pb = pc;
}
if (*pa == i || *pb == i) return true;
else return false;
}int main ()
{
int A [] = {1, 1, 6, 10, 19, 22, 22, 22, 50};
size_t n = sizeof(A)/sizeof(int);
std::cout << contains(A, n, 19) << std::endl; // should print 0
std::cout << contains(A, n, 22) << std::endl; // should print 1
return 0;
}
and, quite honestly, it took me like 10-15 minutes to do it because I have to think through all the pointer arithmetic and corner cases and try to make things compact (and it still isn't compact and probably is incorrect).
Maybe I have poor logical-mathematical ability, but I feel like it takes a lot of brain power just for me to stare at
Code:
template <class BidirectionalIterator, class UnaryPredicate>
BidirectionalIterator partition (BidirectionalIterator first,
BidirectionalIterator last, UnaryPredicate pred)
{
while (first!=last) {
while (pred(*first)) {
++first;
if (first==last) return first;
}
do {
--last;
if (first==last) return first;
} while (!pred(*last));
swap (*first,*last);
++first;
}
return first;
}
(http://www.cplusplus.com/reference/algorithm/partition/) and visualize how everything is happening.
In any event, I love that sort of stuff because when I go to work everything seems easier by comparison. It's like taking the weight off the bat.
Is all this "code golf" stuff actually worth my time, though? I'm a Junior .NET developer and want to make good money some day. Should I be spending all my free time absorbing more .NET knowledge?
Last edited by a moderator: