HackerRank Time Conversion Problem Solution
HackerRank Time Conversion Problem Solution
HackerRank
CLOSE ADS Time Conversion problem CLOSE ADS
solution
YASH PAL March 23, 2021
Search
Subscribe To Channel
Programmingoneonone
Example
s = '12:01:00PM'
Return '12:01:00'.
Return '00:01:00'.
Function Description
Complete the timeConversion function in the editor below. It should return a new string
representing the input time in 24 hour format.
Returns
Input Format
Constraints
HackerRank Diagonal
Difference problem solution
March 23, 2021
import os
import sys
#
# Complete the timeConversion function below.
#
CLOSE ADS CLOSE ADS
def timeConversion(s):
if s[-2:] == "AM" and s[:2] == "12":
return "00" + s[2:-2]
elif s[-2:] == "AM":
return s[:-2]
elif s[-2:] == "PM" and s[:2] == "12":
return s[:-2]
else:
ans = int(s[:2]) + 12
return str(str(ans) + s[2:8])
if __name__ == '__main__':
f = open(os.environ['OUTPUT_PATH'], 'w')
s = input()
result = timeConversion(s)
f.write(str(result) + '\n')
f.close()
void solve(){
int hour, minute, second;
char c1, c2;
scanf("%d:%d:%d%c%c", &hour, &minute, &second, &c1,
&c2);
// printf("%d\n%d\n%d\n%c\n%c", hour, minute, second,
c1, c2);
hour = hour % 12;
if (c1 == 'P'){
hour = hour + 12;
}
printf("%02d:%02d:%02d\n", hour, minute, second);
return;
}
solve();
return 0;
}
int main() {
char t[10];
scanf("%s", t);
if(t[8] == 'P') {
if(t[0] != '1' || t[1] != '2') {
t[0]++;
t[1]+=2;
}
} else {
if(t[0] == '1' && t[1] == '2') {
t[0] = '0';
t[1] = '0';
}
}
t[8] = '\0';
printf("%s\n", t);
return 0;
}
process.stdin.resume();
process.stdin.setEncoding("ascii");
_input = "";
process.stdin.on("data", function (input) {
_input += input;
});
process.stdin.on("end", function () {
processData(_input);
});
Post a Comment
2 Comments
GAJANAN KULKARNI
January 20, 2022 at 11:19 PM
Reply Delete
Replies
Reply