MrPentatonic
Superstar
Anybody coding or learning for black hat purposes?
why does this site load soooooo many scripts @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.
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?
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;
}
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; }
//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));
}
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; }
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;
}
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; }
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
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;
}
Lol nah its just because im avoiding library functions and focusing on the algorithms.Wow C looks crazy complicated. I'm having trouble understanding what any of this means/does.
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
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;
}
Dope idea@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.
I don't want this combined with any other thread. Also, I hope u all contribute.
@typeusername.
Anyway, the thread is for programming information.... and careers/advice.
You should go to school if you want to get into it.... but here's site- I know a guy who is using to get started.
https://www.udemy.com/how-to-become-a-web-developer-from-scratch/