<?php /** * Created by PhpStorm. * User: russ * Date: 11/4/15 * Time: 2:34 PM */ include_once '../helper.php'; function gcd($a, $b) { if ($a % $b === 0) { return $b; } else { return gcd($b, $a % $b); } } //_e_(gcd(36, 12)); function solution_ChocolatesByNumbers($N, $M) { return $N / gcd($N, $M); } _e_(solution_ChocolatesByNumbers(10, 4));
*/ function solution(array $a) { sort($a); $n = count($a); $max_lower = $a[$n - 1] * $a[0] * $a[1]; $max_higher = $a[$n - 1] * $a[$n - 2] * $a[$n - 3]; return max($max_lower, $max_higher); } $samples = [60 => [-3, 1, 2, -2, 5, 6], 125 => [-5, 5, -5, 4], -80 => [-10, -2, -4], 120 => [-4, -6, 3, 4, 5]]; _run_tests_($samples, 'solution'); //foreach ($samples as $expected => $params) { // $got = solution($params); // assert($got === $expected, "Failed. Expected: $expected; Received: $got"); //} _e_('Good job, Russ!'); /** A non-empty zero-indexed array A consisting of N integers is given. The product of triplet (P, Q, R) equates to A[P] * A[Q] * A[R] (0 ≤ P < Q < R < N). For example, array A such that: A[0] = -3 A[1] = 1 A[2] = 2 A[3] = -2 A[4] = 5 A[5] = 6 contains the following example triplets: (0, 1, 2), product is −3 * 1 * 2 = −6 (1, 2, 4), product is 1 * 2 * 5 = 10
} $k += $i; } } $i++; } $semi_primes = []; for ($i = 0; $i < $N + 1; $i++) { $factors_count = 0; $k = $i; while ($f[$k] > 0) { $factors_count++; $k /= $f[$k]; } $semi_primes[$i] = 0; if (++$factors_count === 2) { $semi_primes[$i] = 1; } } // return $semiprimes; $result = []; for ($i = 0, $m = count($P); $i < $m; $i++) { if ($Q[$i] <= $N) { $range = array_slice($semi_primes, $P[$i], $Q[$i] - $P[$i] + 1); $result[] = array_sum($range); } } return $result; } _e_(solution_CountSemiprimes(26, [1, 4, 16], [26, 10, 20]));
} $curr_avg = ($A[$n - 2] + $A[$n - 3]) / 2.0; if ($curr_avg < $min_avg) { $min_avg_pos = $n - 2; } return $min_avg_pos; } $sample = [4, 2, 2, 5, 1, 5, 8]; //_e_($sample); _e_(solution($sample)); $sample = _random_array_(100000, -10000, 10000); //_e_($sample); _e_(solution($sample)); $sample = _random_array_(700, -10000, 10000); //_e_($sample); _e_(solution($sample)); //$sample = [-3, -5, -8, -4, -10]; //_e_($sample); //_e_(solution($sample)); /** A non-empty zero-indexed array A consisting of N integers is given. A pair of integers (P, Q), such that 0 ≤ P < Q < N, is called a slice of array A (notice that the slice contains at least two elements). The average of a slice (P, Q) is the sum of A[P] + A[P + 1] + ... + A[Q] divided by the length of the slice. To be precise, the average equals (A[P] + A[P + 1] + ... + A[Q]) / (Q − P + 1). For example, array A such that: A[0] = 4 A[1] = 2 A[2] = 2 A[3] = 5 A[4] = 1 A[5] = 5 A[6] = 8
function display_rows() { global $post, $id; add_filter('the_title', 'esc_html'); $alt = ''; while (have_posts()) { the_post(); if ($this->is_trash && $post->post_status != 'trash' || !$this->is_trash && $post->post_status == 'trash') { continue; } $alt = 'alternate' == $alt ? '' : 'alternate'; $post_owner = get_current_user_id() == $post->post_author ? 'self' : 'other'; $att_title = _draft_or_post_title(); ?> <tr id='post-<?php echo $id; ?> ' class='<?php echo trim($alt . ' author-' . $post_owner . ' status-' . $post->post_status); ?> ' valign="top"> <?php list($columns, $hidden) = $this->get_column_info(); foreach ($columns as $column_name => $column_display_name) { $class = "class='{$column_name} column-{$column_name}'"; $style = ''; if (in_array($column_name, $hidden)) { $style = ' style="display:none;"'; } $attributes = $class . $style; switch ($column_name) { case 'cb': ?> <th scope="row" class="check-column"><?php if (current_user_can('edit_post', $post->ID)) { ?> <input type="checkbox" name="media[]" value="<?php the_ID(); ?> " /><?php } ?> </th> <?php break; case 'icon': $attributes = 'class="column-icon media-icon"' . $style; ?> <td <?php echo $attributes; ?> ><?php if ($thumb = wp_get_attachment_image($post->ID, array(80, 60), true)) { if ($this->is_trash) { echo $thumb; } else { ?> <a href="<?php echo get_edit_post_link($post->ID, true); ?> " title="<?php echo esc_attr(sprintf(__('Edit "%s"', 'frontend-uploader'), $att_title)); ?> "> <?php echo $thumb; ?> </a> <?php } } ?> </td> <?php break; case 'title': ?> <td <?php echo $attributes; ?> ><strong><?php if ($this->is_trash) { echo $att_title; } else { ?> <a href="<?php echo get_edit_post_link($post->ID, true); ?> " title="<?php echo esc_attr(sprintf(__('Edit "%s"', 'frontend-uploader'), $att_title)); ?> "><?php echo $att_title; ?> </a><?php } _media_states($post); ?> </strong> <p> <?php if (preg_match('/^.*?\\.(\\w+)$/', get_attached_file($post->ID), $matches)) { echo esc_html(strtoupper($matches[1])); } else { echo strtoupper(str_replace('image/', '', get_post_mime_type())); } ?> </p> <?php echo $this->row_actions($this->_get_row_actions($post, $att_title)); ?> </td> <?php break; case 'author': ?> <td <?php echo $attributes; ?> ><?php the_author(); ?> </td> <?php break; case 'tags': ?> <td <?php echo $attributes; ?> ><?php $tags = get_the_tags(); if (!empty($tags)) { $out = array(); foreach ($tags as $c) { $out[] = "<a href='edit.php?tag={$c->slug}'> " . esc_html(sanitize_term_field('name', $c->name, $c->term_id, 'post_tag', 'display')) . "</a>"; } echo join(', ', $out); } else { __('No Tags', 'frontend-uploader'); } ?> </td> <?php break; case 'desc': ?> <td <?php echo $attributes; ?> ><?php echo has_excerpt() ? $post->post_excerpt : ''; ?> </td> <?php break; case 'date': if ('0000-00-00 00:00:00' == $post->post_date && 'date' == $column_name) { $t_time = $h_time = __('Unpublished', 'frontend-uploader'); } else { $t_time = get_the_time(__('Y/m/d g:i:s A', 'frontend-uploader')); $m_time = $post->post_date; $time = get_post_time('G', true, $post, false); if (abs($t_diff = time() - $time) < 86400) { if ($t_diff < 0) { $h_time = sprintf(__('%s from now', 'frontend-uploader'), human_time_diff($time)); } else { $h_time = sprintf(__('%s ago', 'frontend-uploader'), human_time_diff($time)); } } else { $h_time = mysql2date(__('Y/m/d', 'frontend-uploader'), $m_time); } } ?> <td <?php echo $attributes; ?> ><?php echo $h_time; ?> </td> <?php break; case 'parent': if ($post->post_parent > 0) { if (get_post($post->post_parent)) { $title = _draft_or_post_title($post->post_parent); } ?> <td <?php echo $attributes; ?> > <strong><a href="<?php echo get_edit_post_link($post->post_parent); ?> "><?php echo $title; ?> </a></strong>, <?php echo get_the_time(__('Y/m/d', 'frontend-uploader')); ?> </td> <?php } else { ?> <td <?php echo $attributes; ?> ><?php __('(Unattached)', 'frontend-uploader'); ?> <br /> <a class="hide-if-no-js" onclick="findPosts.open( 'media[]','<?php echo $post->ID; ?> ' );return false;" href="#the-list"><?php _e_('Attach', 'frontend-uploader'); ?> </a></td> <?php } break; default: ?> <td <?php echo $attributes; ?> > <?php do_action('manage_fu_media_custom_column', $column_name, $id); ?> </td> <?php break; } } ?> </tr> <?php } }