Where is the flaw in my algorithm for constructing a sorted array of month-year dates given a min and a max?

时间:2016-02-12 20:14:28

标签: javascript arrays algorithm

What I want:

Given two month-year pairs such as (5/2015), (2/2016) I want to construct an array like

%time for i in range(100000): random.choice(a)
%time for i in range(100000): a[random.randint(0,len(a)-1)]
%time for i in range(100000): a[np.random.randint(0,len(a)-1)]
%time for i in range(100000): np.random.choice(a,1)[0]

My algorithm is

[ '5/2015', '6/2015', '7/2015', '8/2015', '9/2015', '10/2015', '11/2015', '12/2015', '1/2016', '2/2016' ]

and after running it with valid var parts1 = DateToMmDdYyyy(new Date(SortedByDate[0].Created)).split('/'), parts2 = DateToMmDdYyyy(new Date(SortedByDate[SortedByDate.length - 1].Created)).split('/'); var y1 = +parts1[2], m1 = +parts1[0], y2 = +parts2[2], m2 = +parts2[0], Labels = new Array(); while ( y1 <= y2 && m1 <= m2 ) { Labels.push(y1 + '/' + m1); if ( m1 < 12 ) { ++m1; } else { ++y1; m1 = 1; } } , y1, m1 and y2 values I am ending up with an empty array m2. Where am I going wrong here? And is it possible to make this more elegant, compact, efficient, readable and correct?

3 个答案:

答案 0 :(得分:3)

The loop condition should be something like

public partial class PersonView : UserControl, IViewFor<Person>
    {
        public PersonView()
        {
            InitializeComponent();
            this.OneWayBind(ViewModel, vm => vm.Name, v => v.PersonName.Text);
        }
      ...
    }

Otherwise, since while ( y1 < y2 || y1 == y2 && m1 <= m2 ) , the loop will not run, even if m2 < m1.

y1 < y2
var y1 = 2015, m1 = 6, 
    y2 = 2016, m2 = 2,
    labels = [];
while ( y1 < y2 || y1 == y2 && m1 <= m2 ) {
  labels.push(y1 + '/' + m1);
  if ( m1 < 12 ) { ++m1; }
  else { ++y1; m1 = 1; }
}
snippet.log(labels);

答案 1 :(得分:1)

Use Date.parse

Loop through the array. concatenate with "0/" + month/year pair.

<html>
<head>
   <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>User Authenticate </title>
</head>
<body> 

<?php

    // Retrieve Post Data
    $username = $_POST["username"];
    $email = $_POST["emailadd"];


         // Set the session information
         session_start();  
         $_SESSION['appusername'] = $username; 
         $_SESSION['appemail'] = $email;

 // Display the Session information
 echo "<h3> Session Data  </h3>";
echo "<table border='1'>";
echo "<tr>
        <td>Username </td>
        <td> Email </td>
      </tr>";
echo "<tr>
        <td>" . $_SESSION['appusername'] . "</td>";
echo  "<td>" . $_SESSION['appemail']. "</td>";
echo   "</tr>";     
 echo "</table>";      

// Provide a button to logout

echo "<form name='logout' method='post' action='logout.php'> 
<input name='btnsubmit' type='submit' value='Logout'> 
</form>";     

?>
</body>
</html>

Date.parse will return timestamp in millisecond. Now you have got integer array. sort it and accordingly change the position of values in original array while you are sorting the timestamp array.

答案 2 :(得分:0)

The first problem I noticed is the m1 <= m2 condition, as 03/2015 is less than 02/2015 but wont match your condition. What you need to do is something like (y1 < y2 || (y1 == y2 && m1 <= m2))

Hope this helps you. Regards