Jump to content

Python time

SamAnw

Hello

I have to write a python program that changes an input file like the following:

1 hour 20 minutes

30 minutes and 2 hours

into an output file like the following:

01:20:00

02:30:00

I have no idea what to do :/ Thankyou!

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, SamAnw said:

Hello

I have to write a python program that changes an input file like the following:

1 hour 20 minutes

30 minutes and 2 hours

into an output file like the following:

01:20:00

02:30:00

I have no idea what to do :/ Thankyou!

If you are allowed to use pre-existing functions that do exactly what you want, take a look at strptime()

Hand, n. A singular instrument worn at the end of the human arm and commonly thrust into somebody’s pocket.

Link to comment
Share on other sites

Link to post
Share on other sites

If you can't do what @WereCatf suggested, look at what the .split() method of strings does. Also take a look at the re module for regular expressions.

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to comment
Share on other sites

Link to post
Share on other sites

Hello everyone,

I need to create a program that converts an input like this one:

1m and 45s
10m,10s
32s, and 12h
76h
1s

Into an output like this one:

00:01:45
00:10:10
12:00:32
76:00:00
00:00:01

 

Does anyone have an idea of how to do this?

 

Thanks in advance

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

Hi @SamAnw, you have started a few threads, all related to the same thing. this questions has already been asked and people offered their help. If you need more help, continue on those threads, rather than starting new ones every 30 minutes.

Link to comment
Share on other sites

Link to post
Share on other sites

9 minutes ago, SamAnw said:

Hello everyone,

I need to create a program that converts an input like this one:

1m and 45s
10m,10s
32s, and 12h
76h
1s

Into an output like this one:

00:01:45
00:10:10
12:00:32
76:00:00
00:00:01

 

Does anyone have an idea of how to do this?

 

Thanks in advance

split the strings at the s an m marks (and h for hour) and place it into a a new string with colons inbetween.

do you not have required reading material for this sort of homework that can be consulted? Because otherwise, Googling stuff effectively is also a trait a programmer should build up.

"We're all in this together, might as well be friends" Tom, Toonami.

 

mini eLiXiVy: my open source 65% mechanical PCB, a build log, PCB anatomy and discussing open source licenses: https://linustechtips.com/topic/1366493-elixivy-a-65-mechanical-keyboard-build-log-pcb-anatomy-and-how-i-open-sourced-this-project/

 

mini_cardboard: a 4% keyboard build log and how keyboards workhttps://linustechtips.com/topic/1328547-mini_cardboard-a-4-keyboard-build-log-and-how-keyboards-work/

Link to comment
Share on other sites

Link to post
Share on other sites

tokenize

normalize

simplify

 

replace comma , dot, : , ; and other characters that could exist with a separator (ex 10h,5m ... you'll want to become "10 h 5 m"

parse the string character by character and add a space or some separator between 0..9 and other characters, to convert 10h to "10 h"

convert everything to lowercase or uppercase, because you don't care about case.

split the text into separate chunks based on that separator (usually space character as most common separator)

 

here's how it would work in php

 

<?php 

$text = "10hours and 5 minutes";

$text = strtolower($text); // make everything lowercase
$text = str_replace( array(',',':',';','-','=','.',chr(0x09)) , ' ',$text); // replace with space (0x09 = tab)

$newText = '';
$prev = false; // previous character was not digit
$offset = 0;
while ($offset<strlen($text)) {
    $c = substr($text,$offset,1);
    if ($c>='0' && $c<='9') {
        if ($prev==false) $newText .= ' ';
        $prev = true; 
    } else {
        if ($prev==true) $newText .= ' ';
        $prev = false;
    } 
    $newText .= $c;
    $offset++;
}
// newText is the proper variable now

$words = explode(' ',$newText);
$filteredWords = array();

foreach ($words as $word) {
    $validWord = false;
    if (strlen($word)>0) {
        if (ctype_digit($word)==true) $validWord=true;
        $testWords = array('day','hour','minute','second');
        foreach ($testWords as $testWord) {
            // first letter of each test word (d, h , m)
            if (strlen($word)==1 && substr($word,0,1)==$testWord) $validWord = true;
            // singular (day, hour, minute, second)
            if (strlen($word)==strlen($testWord) && $word==$testWord) $validWord = true;
            // plural - add S to end
            if ((strlen($word)==(strlen($testWord)+1)) && $word==$testWord.'s') $validWord = true; 
        } 
        // only care about first char if it's not a number
        if (ctype_digit($word)==false && $validWord==true) $word = substr($word,0,1); 
        if ($validWord==true) array_push($filteredWords,$word);
    }
}
// filteredWords array contains all the good stuff  
echo implode(' ',$filteredWords); // just for debugging purposes
// now convert everything to seconds
$total = 0;
if (count($filteredWords)>1) {
    for ($i=1;$i<count($filteredWords);$i++) {
        $a = $filteredWords[$i-1];
        $b = $filteredWords[$i];
        if (ctype_digit($a)==true && ctype_digit($b)==false) {
            $a = intval($a);
            if ($b=='s') $total += $a;
            if ($b=='m') $total += $a*60;
            if ($b=='h') $total += $a*3600;
            if ($b=='d') $total += $a*24*3600;
        }
    }
}
// now total holds the number of seconds 
echo $total;
$units = array(0,0,0,0);
if ($total>0) {
    while ($total >= 86400) { $units[0]++;$total = $total - 86400;}
    while ($total >= 3600) { $units[1]++;$total = $total - 3600;}
    while ($total >= 60) { $units[2]++;$total = $total - 60;}
    $units[3] = $total;
}
// finalText dd:hh:mm:ss

$finalText = '';
for($i=0;$i<4;$i++) $finalText .= ':'.str_pad($units[$i],2,'0',STR_PAD_LEFT);
$finalText = trim($finalText,':');

// as an excercise, figure out how to trim 00: from the front if unneeded

echo $finalText;

die();

ex 10hours and 5 minutes => 00:10:05:00
?>

 

Link to comment
Share on other sites

Link to post
Share on other sites

On 2/14/2020 at 9:36 AM, SamAnw said:

1m and 45s
10m,10s
32s, and 12h
76h
1s

What were you learning in the class that assigned this at the time that it was assigned?

10 times out of 10, the teacher expects nothing but a solution that uses what they've been teaching you.

If this is not homework, just a hobby, the most robust thing to do is formally define the language, and then build a lexer/parser to do the transformation for you.

ENCRYPTION IS NOT A CRIME

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×