Software Development and Programming Careers (Official Discussion Thread)

semtex

:)
Joined
May 1, 2012
Messages
20,310
Reputation
3,396
Daps
46,188
why does this site load soooooo many scripts :damn: @cook @Brooklynzson I keep getting these annoying redirects to those fake "your computer has a virus" pages and it's only after visiting this site.
 

Matt504

YSL as a gang must end
Joined
Sep 7, 2013
Messages
45,221
Reputation
14,767
Daps
274,008
why does this site load soooooo many scripts :damn: @cook @Brooklynzson I keep getting these annoying redirects to those fake "your computer has a virus" pages and it's only after visiting this site.

You need to scan for malware, download "combofix". The coli isn't responsible.
 
Joined
Jan 21, 2015
Messages
53
Reputation
20
Daps
114
Here's another problem from CodingBat I just finished.

"Return true if the string "cat" and "dog" appear the same number of times in the given string."

My solution:

Code:
public boolean catDog(String str) {
  int cat = 0;
  int dog = 0;

  for(int i = 0; i < str.length() - 2; i++){
    if(str.substring(i, i + 3).equals("cat"))
      cat++;
 
    if(str.substring(i, i + 3).equals("dog"))
      dog++;
  }

  return(cat == dog);
}

Anyone else care to share their solution whether it be in Java or another language?

In C.
Code:
bool catdog(char *p)
{
    int counter = 0;

    for (;;) {
        if (*p == 'c') {
            if (*++p == 'a') {
                if (*++p== 't') {
                    ++counter;
                    ++p;
                    continue;
                }
            }
        }
        if (*p == 'd') {
            if (*++p == 'o') {
                if (*++p== 'g') {
                    --counter;
                    ++p;
                    continue;
                }
            }
        }
        if (*p)
            ++p;
        else 
            break;
    }
    return counter == 0 ? true : false;
}
 

Data-Hawk

I have no strings on me.
Joined
May 6, 2012
Messages
8,420
Reputation
1,985
Daps
16,303
Reppin
Oasis
Here is an example of a problem from CodingBat:

"Given a string and an int n, return a string made of n repetitions of the last n characters of the string. You may assume that n is between 0 and the length of the string, inclusive."

Here is my solution:

Code:
public String repeatEnd(String str, int n) {

   String word = "";

  for(int i = n; i > 0; i--){
   for(int j = str.length() - n ; j < str.length(); j++){
      word = word + (str.charAt(j));
  }
   }
   return word;
}

My Solution using C#


Code:
//Example Call
// repeatEnd("Michael", 2, out LastChar);


//Go in n chars, repeat n characters.
static void repeatEnd(string str, int n, out string lastChars)
{
      string str1 = str.Substring(n);    
      lastChars = string.Concat(Enumerable.Repeat(str1, n));
  }


edit: forget to start from the back of the string. this starts from the front. I'll fix after I come back from Target
 
Joined
Jan 21, 2015
Messages
53
Reputation
20
Daps
114
Here is an example of a problem from CodingBat:

"Given a string and an int n, return a string made of n repetitions of the last n characters of the string. You may assume that n is between 0 and the length of the string, inclusive."

Here is my solution:

Code:
public String repeatEnd(String str, int n) {
  
   String word = "";
  
  for(int i = n; i > 0; i--){
   for(int j = str.length() - n ; j < str.length(); j++){
      word = word + (str.charAt(j));
  }
   }
   return word;
}

C.

Code:
char *coli_stringin(char *str, int n)
{
    char buf[n];
    char *tmp = malloc(n*n + 1);
    char *ret = tmp;

    int size = 0;
    while (str[size]) {
        ++size;
    }

    str += (size - n);

    for (int i = 0; i < n; ++i) {
        buf[i] = *str++;
    }

    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            *tmp++ = buf[j];
        }
    }
    *++tmp = '\0';
    return ret;
}
 

Double J

Banned
Joined
May 11, 2012
Messages
1,929
Reputation
-665
Daps
5,264
In C.
Code:
bool catdog(char *p)
{
    int counter = 0;

    for (;;) {
        if (*p == 'c') {
            if (*++p == 'a') {
                if (*++p== 't') {
                    ++counter;
                    ++p;
                    continue;
                }
            }
        }
        if (*p == 'd') {
            if (*++p == 'o') {
                if (*++p== 'g') {
                    --counter;
                    ++p;
                    continue;
                }
            }
        }
        if (*p)
            ++p;
        else
            break;
    }
    return counter == 0 ? true : false;
}

C.

Code:
char *coli_stringin(char *str, int n)
{
    char buf[n];
    char *tmp = malloc(n*n + 1);
    char *ret = tmp;

    int size = 0;
    while (str[size]) {
        ++size;
    }

    str += (size - n);

    for (int i = 0; i < n; ++i) {
        buf[i] = *str++;
    }

    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            *tmp++ = buf[j];
        }
    }
    *++tmp = '\0';
    return ret;
}

Wow C looks crazy complicated. I'm having trouble understanding what any of this means/does.
 

Double J

Banned
Joined
May 11, 2012
Messages
1,929
Reputation
-665
Daps
5,264
My Solution using C#


Code:
//Example Call
// repeatEnd("Michael", 2, out LastChar);


//Go in n chars, repeat n characters.
static void repeatEnd(string str, int n, out string lastChars)
{
      string str1 = str.Substring(n);   
      lastChars = string.Concat(Enumerable.Repeat(str1, n));
  }


edit: forget to start from the back of the string. this starts from the front. I'll fix after I come back from Target

Haha MUCH easier to understand than C.
 

Matt504

YSL as a gang must end
Joined
Sep 7, 2013
Messages
45,221
Reputation
14,767
Daps
274,008
@Brooklynzson, I think it would be very valuable and useful to a lot of Coli members if we could have an entire section dedicated to programming, we could get a lot of people interested in it and help those who are already in the field. One of the many benefits would be a separation of concerns, threads dedicated to each programming language with the section.

just a suggestion.
 

Double J

Banned
Joined
May 11, 2012
Messages
1,929
Reputation
-665
Daps
5,264
This is one I found to be pretty challenging:

"Given a string, return the sum of the numbers appearing in the string, ignoring all other characters. A number is a series of 1 or more digit chars in a row."

Examples:

sumNumbers("abc123xyz") → 123
sumNumbers("aa11b33") → 44
sumNumbers("7 11") → 18

Here's my code:

Code:
public int sumNumbers(String str) {
  String temp = "";
  int total = 0;
 
  for(int i = 0; i < str.length(); i++){
    if(Character.isDigit(str.charAt(i)) == true)
      temp = temp + str.charAt(i);
 
    if(Character.isDigit(str.charAt(i)) == false  && !temp.equals("")){

      total += Integer.parseInt(temp);
      temp = "";
    }
   
    if(i == str.length() - 1 && !temp.equals(""))
      total += Integer.parseInt(temp);
  }
 
  return total;
 
 
}

Probably not the cleanest solution but it works :manny:
 
Joined
Jan 21, 2015
Messages
53
Reputation
20
Daps
114
This is one I found to be pretty challenging:

"Given a string, return the sum of the numbers appearing in the string, ignoring all other characters. A number is a series of 1 or more digit chars in a row."

Examples:

sumNumbers("abc123xyz") → 123
sumNumbers("aa11b33") → 44
sumNumbers("7 11") → 18

Here's my code:

Code:
public int sumNumbers(String str) {
  String temp = "";
  int total = 0;

  for(int i = 0; i < str.length(); i++){
    if(Character.isDigit(str.charAt(i)) == true)
      temp = temp + str.charAt(i);

    if(Character.isDigit(str.charAt(i)) == false  && !temp.equals("")){

      total += Integer.parseInt(temp);
      temp = "";
    }
 
    if(i == str.length() - 1 && !temp.equals(""))
      total += Integer.parseInt(temp);
  }

  return total;


}

Probably not the cleanest solution but it works :manny:

Heres a quick one in C. I check from the end of the string (right to left) which make things easier. If the characters ASCII code is 47<x<58 then it is a digit (0-9). To get the digit you minus 48 from the characters ASCII code. For example a 5 would be ASCII 53, thus 53-48=5. Since we are going right to left, the first digit of a number is the ones column, the second is the tens column, the third is the hundreds etc... So a number such as 173 = (3*1) + (7*10) + (1*100).

edit: made simpler

Code:
int strsum(char *str)
{
    int sum = 0;
    int len = 0;

    while (str[len])
        ++len;

    for (int place = 1;len > 0; --len, place = 1) {
        while (str[len] < 58 && str[len] > 47) {
            sum += (str[len--] - 48) * place;
            place *= 10;
        }
    }
    return sum;
}
 
Last edited:

Golayitdown

Veteran
Joined
Apr 30, 2012
Messages
24,418
Reputation
4,836
Daps
82,227
@Brooklynzson, I think it would be very valuable and useful to a lot of Coli members if we could have an entire section dedicated to programming, we could get a lot of people interested in it and help those who are already in the field. One of the many benefits would be a separation of concerns, threads dedicated to each programming language with the section.

just a suggestion.
Dope idea
 

Scott Larock

Its hard leaving thecoli but I gotta find a way...
Joined
Mar 11, 2013
Messages
8,679
Reputation
365
Daps
18,023
Reppin
Hell
Top