MPMoviePlayerController currentPlaybackTime jump while set setCurrentPlaybackTime - mpmovieplayercontroller

I am using MPMoviePlayerController as my media player.
I am trying to seek by using my method (movieController is an instance of MPMoviePlayerController)
-(void)seekToTime:(NSTimeInterval)time
{
[movieController setCurrentPlaybackTime:time];
[NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:#selector(printCurrentPlaybacktime) userInfo:nil repeats:YES];
}
as you can see - after seeking I am print out the current playback time rapidly.
The problem is this - at start the current playback time is the actual new time I set.
after some time the current playback time is the old time before changing the playback time,
and after a while it's turn back to the new actual time. the result for the user is that the seek slider is jumping.
this is the log
2012-10-02 11:58:24.317 Comigo[1067:907] print [movieController currentPlaybackTime] = 3662.729736
2012-10-02 11:58:24.321 Comigo[1067:907] print [movieController currentPlaybackTime] = 3662.729736
2012-10-02 11:58:24.368 Comigo[1067:907] print [movieController currentPlaybackTime] = 3662.729736
2012-10-02 11:58:24.370 Comigo[1067:907] print [movieController currentPlaybackTime] = 3662.729736
2012-10-02 11:58:24.372 Comigo[1067:907] print [movieController currentPlaybackTime] = 3662.729736
2012-10-02 11:58:24.374 Comigo[1067:907] [MPAVController] Autoplay: _streamUnlikelyToKeepUp: 1 -> 0
2012-10-02 11:58:24.375 Comigo[1067:907] [MPAVController] Autoplay: Skipping autoplay, disabled (for current item: 1, on player: 1)
2012-10-02 11:58:24.376 Comigo[1067:907] [MPAVController] Autoplay: _streamRanDry: 0 -> 1
2012-10-02 11:58:24.380 Comigo[1067:907] [MPAVController] Autoplay: Took background task assertion (9) for playback stall
2012-10-02 11:58:24.382 Comigo[1067:907] [MPAVController] Autoplay: Skipping autoplay, disabled (for current item: 1, on player: 1)
2012-10-02 11:58:24.383 Comigo[1067:907] print [movieController currentPlaybackTime] = 3662.729736
2012-10-02 11:58:24.385 Comigo[1067:907] print [movieController currentPlaybackTime] = 3662.729736
2012-10-02 11:58:24.386 Comigo[1067:907] print [movieController currentPlaybackTime] = 3662.729736
2012-10-02 11:58:24.387 Comigo[1067:907] print [movieController currentPlaybackTime] = 3662.729736
2012-10-02 11:58:24.388 Comigo[1067:907] print [movieController currentPlaybackTime] = 3662.729736
2012-10-02 11:58:24.391 Comigo[1067:907] print [movieController currentPlaybackTime] = 3662.729736
2012-10-02 11:58:24.395 Comigo[1067:907] print [movieController currentPlaybackTime] = 3662.729736
2012-10-02 11:58:24.398 Comigo[1067:907] print [movieController currentPlaybackTime] = 3662.729736
2012-10-02 11:58:24.402 Comigo[1067:907] print [movieController currentPlaybackTime] = 3662.729736
2012-10-02 11:58:24.405 Comigo[1067:907] print [movieController currentPlaybackTime] = 3662.729736
2012-10-02 11:58:24.408 Comigo[1067:907] print [movieController currentPlaybackTime] = 3662.729736
2012-10-02 11:58:24.412 Comigo[1067:907] print [movieController currentPlaybackTime] = 3662.729736
2012-10-02 11:58:24.415 Comigo[1067:907] print [movieController currentPlaybackTime] = 3662.729736
2012-10-02 11:58:24.419 Comigo[1067:907] print [movieController currentPlaybackTime] = 3662.729736
2012-10-02 11:58:24.423 Comigo[1067:907] print [movieController currentPlaybackTime] = 3662.729736
2012-10-02 11:58:24.425 Comigo[1067:907] print [movieController currentPlaybackTime] = 3662.729736
2012-10-02 11:58:24.428 Comigo[1067:907] print [movieController currentPlaybackTime] = 3662.729736
2012-10-02 11:58:24.431 Comigo[1067:907] print [movieController currentPlaybackTime] = 3662.729736
2012-10-02 11:58:24.435 Comigo[1067:907] print [movieController currentPlaybackTime] = 3662.729736
2012-10-02 11:58:24.438 Comigo[1067:907] print [movieController currentPlaybackTime] = 3662.729736
2012-10-02 11:58:24.442 Comigo[1067:907] print [movieController currentPlaybackTime] = 3662.729736
2012-10-02 11:58:24.445 Comigo[1067:907] print [movieController currentPlaybackTime] = 3662.729736
2012-10-02 11:58:24.448 Comigo[1067:907] print [movieController currentPlaybackTime] = 3662.729736
2012-10-02 11:58:24.451 Comigo[1067:907] print [movieController currentPlaybackTime] = 3662.729736
2012-10-02 11:58:24.455 Comigo[1067:907] print [movieController currentPlaybackTime] = 3662.729736
2012-10-02 11:58:24.459 Comigo[1067:907] print [movieController currentPlaybackTime] = 3662.729736
2012-10-02 11:58:24.461 Comigo[1067:907] print [movieController currentPlaybackTime] = 3662.729736
2012-10-02 11:58:24.465 Comigo[1067:907] print [movieController currentPlaybackTime] = 3662.729736
2012-10-02 11:58:24.466 Comigo[1067:907] [MPAVController] Autoplay: Skipping autoplay, disabled (for current item: 1, on player: 1)
2012-10-02 11:58:24.469 Comigo[1067:907] [MPAVController] Autoplay: Enabling autoplay
2012-10-02 11:58:24.470 Comigo[1067:907] print [movieController currentPlaybackTime] = 956.568935
2012-10-02 11:58:24.472 Comigo[1067:907] print [movieController currentPlaybackTime] = 956.568935
2012-10-02 11:58:24.482 Comigo[1067:907] print [movieController currentPlaybackTime] = 956.568935
2012-10-02 11:58:24.483 Comigo[1067:907] print [movieController currentPlaybackTime] = 956.568935
2012-10-02 11:58:24.484 Comigo[1067:907] print [movieController currentPlaybackTime] = 956.568935
2012-10-02 11:58:24.487 Comigo[1067:907] print [movieController currentPlaybackTime] = 956.568935
2012-10-02 11:58:24.489 Comigo[1067:907] print [movieController currentPlaybackTime] = 956.568935
2012-10-02 11:58:24.490 Comigo[1067:907] print [movieController currentPlaybackTime] = 956.568935
2012-10-02 11:58:24.495 Comigo[1067:907] print [movieController currentPlaybackTime] = 956.568935
2012-10-02 11:58:24.498 Comigo[1067:907] print [movieController currentPlaybackTime] = 956.568935
2012-10-02 11:58:24.501 Comigo[1067:907] print [movieController currentPlaybackTime] = 956.568935
2012-10-02 11:58:24.505 Comigo[1067:907] print [movieController currentPlaybackTime] = 956.568935
2012-10-02 11:58:24.508 Comigo[1067:907] print [movieController currentPlaybackTime] = 956.568935
2012-10-02 11:58:24.511 Comigo[1067:907] print [movieController currentPlaybackTime] = 956.568935
2012-10-02 11:58:24.515 Comigo[1067:907] print [movieController currentPlaybackTime] = 956.568935
2012-10-02 11:58:24.519 Comigo[1067:907] print [movieController currentPlaybackTime] = 956.568935
2012-10-02 11:58:24.521 Comigo[1067:907] print [movieController currentPlaybackTime] = 956.568935
2012-10-02 11:58:24.525 Comigo[1067:907] print [movieController currentPlaybackTime] = 956.568935
2012-10-02 11:58:24.528 Comigo[1067:907] print [movieController currentPlaybackTime] = 956.568935
2012-10-02 11:58:24.531 Comigo[1067:907] print [movieController currentPlaybackTime] = 956.568935
2012-10-02 11:58:24.535 Comigo[1067:907] print [movieController currentPlaybackTime] = 956.568935
2012-10-02 11:58:24.538 Comigo[1067:907] print [movieController currentPlaybackTime] = 956.568935
2012-10-02 11:58:24.541 Comigo[1067:907] print [movieController currentPlaybackTime] = 956.568935
2012-10-02 11:58:24.545 Comigo[1067:907] print [movieController currentPlaybackTime] = 956.568935
2012-10-02 11:58:24.548 Comigo[1067:907] print [movieController currentPlaybackTime] = 956.568935
2012-10-02 11:58:24.551 Comigo[1067:907] print [movieController currentPlaybackTime] = 956.568935
2012-10-02 11:58:24.555 Comigo[1067:907] print [movieController currentPlaybackTime] = 956.568935
2012-10-02 11:58:25.808 Comigo[1067:907] print [movieController currentPlaybackTime] = 956.568935
2012-10-02 11:58:27.061 Comigo[1067:907] print [movieController currentPlaybackTime] = 956.568935
2012-10-02 11:58:27.065 Comigo[1067:907] print [movieController currentPlaybackTime] = 956.568935
2012-10-02 11:58:27.068 Comigo[1067:907] print [movieController currentPlaybackTime] = 956.568935
2012-10-02 11:58:27.071 Comigo[1067:907] print [movieController currentPlaybackTime] = 956.568935
2012-10-02 11:58:27.075 Comigo[1067:907] print [movieController currentPlaybackTime] = 956.568935
2012-10-02 11:58:27.079 Comigo[1067:907] print [movieController currentPlaybackTime] = 956.568935
2012-10-02 11:58:27.081 Comigo[1067:907] print [movieController currentPlaybackTime] = 956.568935
2012-10-02 11:58:27.085 Comigo[1067:907] print [movieController currentPlaybackTime] = 956.568935
2012-10-02 11:58:27.088 Comigo[1067:907] print [movieController currentPlaybackTime] = 956.568935
2012-10-02 11:58:27.091 Comigo[1067:907] print [movieController currentPlaybackTime] = 956.568935
2012-10-02 11:58:27.095 Comigo[1067:907] print [movieController currentPlaybackTime] = 956.568935
2012-10-02 11:58:27.099 Comigo[1067:907] print [movieController currentPlaybackTime] = 956.568935
2012-10-02 11:58:27.101 Comigo[1067:907] print [movieController currentPlaybackTime] = 956.568935
2012-10-02 11:58:27.106 Comigo[1067:907] print [movieController currentPlaybackTime] = 956.568935
2012-10-02 11:58:27.108 Comigo[1067:907] print [movieController currentPlaybackTime] = 956.568935
2012-10-02 11:58:27.111 Comigo[1067:907] print [movieController currentPlaybackTime] = 956.568935
2012-10-02 11:58:27.115 Comigo[1067:907] print [movieController currentPlaybackTime] = 956.568935
2012-10-02 11:58:27.117 Comigo[1067:907] currentTime 956.568935
2012-10-02 11:58:27.120 Comigo[1067:907] print [movieController currentPlaybackTime] = 956.568935
2012-10-02 11:58:27.269 Comigo[1067:907] print [movieController currentPlaybackTime] = 956.568935
2012-10-02 11:58:27.271 Comigo[1067:907] print [movieController currentPlaybackTime] = 956.568935
2012-10-02 11:58:27.275 Comigo[1067:907] print [movieController currentPlaybackTime] = 956.568935
2012-10-02 11:58:27.278 Comigo[1067:907] print [movieController currentPlaybackTime] = 956.568935
2012-10-02 11:58:27.281 Comigo[1067:907] print [movieController currentPlaybackTime] = 956.568935
2012-10-02 11:58:27.285 Comigo[1067:907] print [movieController currentPlaybackTime] = 956.568935
2012-10-02 11:58:27.288 Comigo[1067:907] print [movieController currentPlaybackTime] = 956.568935
2012-10-02 11:58:27.291 Comigo[1067:907] print [movieController currentPlaybackTime] = 956.568935
2012-10-02 11:58:27.295 Comigo[1067:907] print [movieController currentPlaybackTime] = 956.568935
2012-10-02 11:58:27.298 Comigo[1067:907] print [movieController currentPlaybackTime] = 956.568935
2012-10-02 11:58:27.301 Comigo[1067:907] print [movieController currentPlaybackTime] = 956.568935
2012-10-02 11:58:27.305 Comigo[1067:907] print [movieController currentPlaybackTime] = 956.568935
2012-10-02 11:58:27.308 Comigo[1067:907] print [movieController currentPlaybackTime] = 956.568935
2012-10-02 11:58:27.311 Comigo[1067:907] print [movieController currentPlaybackTime] = 956.568935
2012-10-02 11:58:27.315 Comigo[1067:907] print [movieController currentPlaybackTime] = 956.568935
2012-10-02 11:58:27.318 Comigo[1067:907] print [movieController currentPlaybackTime] = 956.568935
2012-10-02 11:58:27.321 Comigo[1067:907] print [movieController currentPlaybackTime] = 956.568935
2012-10-02 11:58:27.325 Comigo[1067:907] print [movieController currentPlaybackTime] = 956.568935
2012-10-02 11:58:27.328 Comigo[1067:907] print [movieController currentPlaybackTime] = 956.568935
2012-10-02 11:58:27.331 Comigo[1067:907] print [movieController currentPlaybackTime] = 956.568935
2012-10-02 11:58:27.335 Comigo[1067:907] print [movieController currentPlaybackTime] = 956.568935
2012-10-02 11:58:27.338 Comigo[1067:907] print [movieController currentPlaybackTime] = 956.568935
2012-10-02 11:58:27.341 Comigo[1067:907] print [movieController currentPlaybackTime] = 956.568935
2012-10-02 11:58:27.345 Comigo[1067:907] print [movieController currentPlaybackTime] = 956.568935
2012-10-02 11:58:27.348 Comigo[1067:907] print [movieController currentPlaybackTime] = 956.568935
2012-10-02 11:58:27.351 Comigo[1067:907] print [movieController currentPlaybackTime] = 956.568935
2012-10-02 11:58:27.355 Comigo[1067:907] print [movieController currentPlaybackTime] = 956.568935
2012-10-02 11:58:27.358 Comigo[1067:907] print [movieController currentPlaybackTime] = 956.568935
2012-10-02 11:58:27.361 Comigo[1067:907] print [movieController currentPlaybackTime] = 956.568935
2012-10-02 11:58:27.365 Comigo[1067:907] print [movieController currentPlaybackTime] = 956.568935
2012-10-02 11:58:27.369 Comigo[1067:907] print [movieController currentPlaybackTime] = 956.568935
2012-10-02 11:58:27.372 Comigo[1067:907] print [movieController currentPlaybackTime] = 956.568935
2012-10-02 11:58:27.375 Comigo[1067:907] print [movieController currentPlaybackTime] = 956.568935
2012-10-02 11:58:27.378 Comigo[1067:907] print [movieController currentPlaybackTime] = 956.568935
2012-10-02 11:58:27.381 Comigo[1067:907] print [movieController currentPlaybackTime] = 956.568935
2012-10-02 11:58:27.385 Comigo[1067:907] print [movieController currentPlaybackTime] = 956.568935
2012-10-02 11:58:27.388 Comigo[1067:907] print [movieController currentPlaybackTime] = 956.568935
2012-10-02 11:58:27.391 Comigo[1067:907] print [movieController currentPlaybackTime] = 956.568935
2012-10-02 11:58:27.395 Comigo[1067:907] print [movieController currentPlaybackTime] = 956.568935
2012-10-02 11:58:27.398 Comigo[1067:907] print [movieController currentPlaybackTime] = 956.568935
2012-10-02 11:58:27.401 Comigo[1067:907] print [movieController currentPlaybackTime] = 956.568935
2012-10-02 11:58:27.405 Comigo[1067:907] print [movieController currentPlaybackTime] = 956.568935
2012-10-02 11:58:27.408 Comigo[1067:907] print [movieController currentPlaybackTime] = 956.568935
2012-10-02 11:58:27.411 Comigo[1067:907] print [movieController currentPlaybackTime] = 956.568935
2012-10-02 11:58:27.415 Comigo[1067:907] print [movieController currentPlaybackTime] = 956.568935
2012-10-02 11:58:27.419 Comigo[1067:907] print [movieController currentPlaybackTime] = 956.568935
2012-10-02 11:58:27.421 Comigo[1067:907] print [movieController currentPlaybackTime] = 956.568935
2012-10-02 11:58:27.425 Comigo[1067:907] print [movieController currentPlaybackTime] = 3660.000000
2012-10-02 11:58:27.429 Comigo[1067:907] print [movieController currentPlaybackTime] = 3660.000000
2012-10-02 11:58:27.431 Comigo[1067:907] print [movieController currentPlaybackTime] = 3660.000000
2012-10-02 11:58:27.448 Comigo[1067:907] print [movieController currentPlaybackTime] = 3660.000000
2012-10-02 11:58:27.449 Comigo[1067:907] print [movieController currentPlaybackTime] = 3660.000000
2012-10-02 11:58:27.450 Comigo[1067:907] print [movieController currentPlaybackTime] = 3660.000000
2012-10-02 11:58:27.455 Comigo[1067:907] print [movieController currentPlaybackTime] = 3660.000000
2012-10-02 11:58:27.459 Comigo[1067:907] print [movieController currentPlaybackTime] = 3660.000000
2012-10-02 11:58:27.461 Comigo[1067:907] print [movieController currentPlaybackTime] = 3660.000000
2012-10-02 11:58:27.465 Comigo[1067:907] print [movieController currentPlaybackTime] = 3660.000000
2012-10-02 11:58:27.468 Comigo[1067:907] print [movieController currentPlaybackTime] = 3660.000000
2012-10-02 11:58:27.472 Comigo[1067:907] print [movieController currentPlaybackTime] = 3660.000000
2012-10-02 11:58:27.475 Comigo[1067:907] print [movieController currentPlaybackTime] = 3660.000000
2012-10-02 11:58:27.478 Comigo[1067:907] print [movieController currentPlaybackTime] = 3660.000000
2012-10-02 11:58:27.481 Comigo[1067:907] print [movieController currentPlaybackTime] = 3660.000000
2012-10-02 11:58:27.485 Comigo[1067:907] print [movieController currentPlaybackTime] = 3660.000000
2012-10-02 11:58:27.489 Comigo[1067:907] print [movieController currentPlaybackTime] = 3660.000000
2012-10-02 11:58:27.491 Comigo[1067:907] print [movieController currentPlaybackTime] = 3660.000000
2012-10-02 11:58:27.495 Comigo[1067:907] print [movieController currentPlaybackTime] = 3660.000000
2012-10-02 11:58:27.498 Comigo[1067:907] print [movieController currentPlaybackTime] = 3660.000000
2012-10-02 11:58:27.501 Comigo[1067:907] print [movieController currentPlaybackTime] = 3660.000000
2012-10-02 11:58:27.505 Comigo[1067:907] print [movieController currentPlaybackTime] = 3660.000000
2012-10-02 11:58:27.509 Comigo[1067:907] print [movieController currentPlaybackTime] = 3660.000000
2012-10-02 11:58:27.511 Comigo[1067:907] print [movieController currentPlaybackTime] = 3660.000000
2012-10-02 11:58:27.515 Comigo[1067:907] print [movieController currentPlaybackTime] = 3660.000000
2012-10-02 11:58:27.518 Comigo[1067:907] print [movieController currentPlaybackTime] = 3660.000000
2012-10-02 11:58:27.521 Comigo[1067:907] print [movieController currentPlaybackTime] = 3660.000000
2012-10-02 11:58:27.525 Comigo[1067:907] print [movieController currentPlaybackTime] = 3660.000000
2012-10-02 11:58:27.529 Comigo[1067:907] print [movieController currentPlaybackTime] = 3660.000000
2012-10-02 11:58:27.531 Comigo[1067:907] print [movieController currentPlaybackTime] = 3660.000000
2012-10-02 11:58:27.535 Comigo[1067:907] print [movieController currentPlaybackTime] = 3660.000000
2012-10-02 11:58:27.538 Comigo[1067:907] print [movieController currentPlaybackTime] = 3660.000000}
I delete some rows (because there is a lot of them). so don't pay attention to the log date.
thanks a lot

Why don't you just use
[movieController pause]
instead?

Related

Battleship game - how to let players place their boats?

I'm creating a battleship game in Ruby. Currently, players have 2 boats (size 3 and size 4) and I arbitrarily placed the boats by myself in the code.
Each player has its own board (array1/array2): I put a 0 when the spot is empty and 1 when there's a boat.
My question is, how can I make the players to place their ships on their board by themselves?
My idea was to ask them to enter the first point of each boat and then ask them the side (north, east, south and west) to set the orientation.
Obviously, a ship can't be placed out of bounds nor on the same space as another ship.
How can I make sure of that with my code? I have no idea where to start...
Thanks!
# Board for player 1
board1 = []
for i in 0..4
board1[i] = []
(0..4).each do
board1[i].append('O')
end
end
# Board for player 2
board2 = []
for i in 0..4
board2[i] = []
(0..4).each do
board2[i].append('O')
end
end
# Display the boards
def display_board(board)
for row in board
puts row.map { |k| "#{k}" }.join(' ')
end
end
# Generation of the player boards
array1 = [ [0, 1, 1, 1, 0], [1, 0, 0, 0, 0], [1, 0, 0, 0, 0], [1, 0, 0, 0, 0], [1, 0, 0, 0, 0] ]
array2 = [ [0, 0, 0, 0, 0], [0, 0, 1, 0, 0], [0, 0, 1, 0, 1], [0, 0, 1, 0, 1], [0, 0, 1, 0, 1] ]
# A player wins when he/she reaches 7 points (2 ships each to place on their grid : a small ship (3x1 side) and a large one (4x1 size))
solution = 7
# We set the number of points at 0 at the start of the game
wins_p1 = 0
wins_p2 = 0
#Starting of the game and printing the board
while true do
puts 'Welcome!'
puts 'Enter start or stop'
starting = gets.chomp
puts "\n"
case starting.downcase
when 'start'
puts 'Enter the name of P1'
name_p1 = gets.chomp
puts "\n"
puts 'Enter the name of P2'
name_p2 = gets.chomp
puts "\n"
while wins_p1 < solution || wins_p2 < solution
puts "--- #{name_p1.upcase}'S TURN ---"
puts 'Enter line number (between 1 and 5):'
row_p1 = gets.chomp.to_i-1
puts 'Enter column number (between 1 and 5):'
column_p1 = gets.chomp.to_i-1
case array1[row_p1][column_p1]
when 1
board1[row_p1][column_p1] = 'X'
wins_p1 += 1
when 0
board1[row_p1][column_p1] = '-'
when 'X', '-'
next
end
puts "\n"
puts '--------------'
display_board(board1)
puts '--------------'
puts '----------------------'
puts "#{name_p1} has #{wins_p1} point#{"s" if wins_p1 > 1}."
puts '----------------------'
puts "\n"
break if wins_p1 == solution
puts "--- #{name_p2.upcase}'S TURN ---"
puts 'Enter line number (between 1 and 5):'
row_p2 = gets.chomp.to_i-1
puts 'Enter column number (between 1 and 5):'
column_p2 = gets.chomp.to_i-1
case array2[row_p2][column_p2]
when 1
board2[row_p2][column_p2] = 'X'
wins_p2 += 1
when 0
board2[row_p2][column_p2] = '-'
when 'X', '-'
next
end
puts "\n"
puts '--------------'
display_board(board2)
puts '--------------'
puts '----------------------'
puts "#{name_p2} a #{wins_p2} point#{"s" if wins_p2 > 1}."
puts '----------------------'
puts "\n"
break if wins_p2 == solution
end
puts "#{name_p1}, you are the winner!" if wins_p1 == solution
puts "#{name_p2}, you are the winner!" if wins_p2 == solution
puts "\n"
break
when 'stop'
puts 'See you soon!'
break
else
puts 'Enter start or stop only!'
puts "\n"
end
end
Going with your idea.
Get user input (x,y) for start of ship
Make sure that space is free (0)
Create a function that checks if a ship of size N (3 or 4) would fit in each orientation. For example in pseudocode (and this can be generalized with a direction parameter):
def wouldFitEast(board, anArray, posX, posY):
steps = 0
while (steps < N)
posX = posX + 1
if !validArrayIndex(posX, posY) or anArray[posX][posY] == 1
return false;
steps = steps + 1
return true;
if there is at least one direction the ship will fit, then ask which direction the user would like, otherwise, they need to pick a different location

Python 3.x runs in IDLE but has issues in Pycharm

I'm new to Stack Overflow and Python.
I've been teaching myself Python 3 for the last couple of weeks using IDLE to create my code which has been working great. I recently decided to download and install the community version of PyCharm. Here's my issue...
I've been using the import random and random.randint() command to generate numbers. When I opened my last Python project, which worked fine in IDLE, into PyCharm and tried to run it, it gave me back two errors.
When I use the "Debug" command in PyCharm, the program runs fine but when I use the "Run" function it gives me two errors.
Here's my code:
import random
# Megamillion Generator
def mega():
s = []
a = random.randint(start, finish)
b = random.randint(start, finish)
c = random.randint(start, finish)
d = random.randint(start, finish)
e = random.randint(start, finish)
s.append(a)
s.append(b)
s.append(c)
s.append(d)
s.append(e)
s.sort()
if x < 10:
print("Random ", str(i), " ", end="")
print(s)
print("Sorted ", end="")
print(end=" ")
print(s)
else:
print("Random", str(i), end="")
print(s)
s.sort()
print("Sorted ", end="")
print(end="")
print(s)
return s
# 3 digit Generator
def digit_3():
s = []
a = random.randint(0, 9)
b = random.randint(0, 9)
c = random.randint(0, 9)
s.append(a)
s.append(b)
s.append(c)
print(format, "Random {0:2}")
print((i), end="")
print(s)
s.sort()
print("Sorted", end="")
print(end=" ")
print(s)
return s
# 4 digit Generator
def digit_4():
s = []
a = random.randint(0, 9)
b = random.randint(0, 9)
c = random.randint(0, 9)
d = random.randint(0, 9)
s.append(a)
s.append(b)
s.append(c)
s.append(d)
print("Random ", end="")
print((i), end="")
print(s)
s.sort()
print("Sorted", end="")
print(end=" ")
print(s)
return s
# Begin Generator
gentype = str(input("Enter '3' for 3-digit, '4' for 4-digit, 'm' for Megamillion or 'all' for all three"))
x = int(input("Enter a number "))
x = x + 1
if gentype == '3':
for i in range(1, x):
digit_3()
elif gentype == '4':
for i in range(1, x):
digit_4()
else:
start = int(input("Enter smallest number to generate "))
finish = int(input("Enter highest number to generate "))
for i in range(1, x):
mega()
And here's the error msg:
line 1, in <module>
import random
line 5, in <module>
a=random.randint(1,70)
AttributeError: module 'random' has no attribute 'randint'
Any Help would be greatly appreciated.

calculate fibonacci sequence position from user inserted value

Below is my full program's code:
GradeAvg = [[59, 'F'], [69, 'D'], [79, 'C'], [89, 'B'], [100, 'A']]
def letter_grade
num = number_grade
_, letter = GradeAvg.find { |n, _| num <= n }
[num, letter]
end
def number_grade
loop do
puts 'Please insert a number between 1 and 100.'
num = gets.to_i
break(num) if (1..100).cover?(num)
end
end
def fib(n)
return n if n < 2
fib(n-1) + fib(n-2)
end
10.times { print letter_grade; (num).each { |n| puts fib(n) }; puts }
It is failing with the below error:
undefined local variable or method `num' for main:Object (NameError)
Why can't I apply my fibonacci sequence calculation on my acceptable user inserted value in variable num?
You're trying to access num which is assigned in letter_grade. You must use the return value of the function call (ie num, letter = letter_grade) to read the result.
It should also be noted that Fixnum#each (ie (num).each) is not a method. 10.times { ... } already makes a loop though, so I think this was just an oversight on your part.
See the bolded section for changes. I also changed your fibonacci function to calculate in linear time (instead of exponential time)
GradeAvg = [[59, 'F'], [69, 'D'], [79, 'C'], [89, 'B'], [100, 'A']]
def number_grade
loop do
print "Please insert a number between 1 and 100."
num = gets.to_i
break(num) if (1..100).cover?(num)
end
end
def letter_grade
num = number_grade
_, letter = GradeAvg.find { |n, _| num <= n }
[num, letter]
end
def fib (n, a = 0, b = 1)
if n == 0 then
a
else
fib n - 1, b, a + b
end
end
10.times do
num, letter = letter_grade
puts letter
puts (fib num)
end
Example program execution
# Please insert a number between 1 and 100. 95
# A
# 31940434634990099905
# Please insert a number between 1 and 100. 87
# B
# 679891637638612258
# Please insert a number between 1 and 100. 77
# C
# 5527939700884757
# Please insert a number between 1 and 100. 66
# D
# 27777890035288
# Please insert a number between 1 and 100. 55
# F
# 139583862445
# Please insert a number between 1 and 100. 10
# F
# 55
# Please insert a number between 1 and 100. ...
# ...
Try like this:
GradeAvg = [[59, 'F'], [69, 'D'], [79, 'C'], [89, 'B'], [100, 'A']]
def letter_grade
#num = number_grade
_, letter = GradeAvg.find { |n, _| #num <= n }
[#num, letter]
end
def number_grade
loop do
puts 'Please insert a number between 1 and 100.'
#num = gets.to_i
break(#num) if (1..100).cover?(#num)
end
end
def fib(n)
return n if n < 2
fib(n-1) + fib(n-2)
end
10.times { print letter_grade; puts fib(#num)}

Gnome sort is faster than quick sort?

I decided to delve into sorting algorithms and implemented a few such as bubble, selection, gnome, insertion, merge and quick sort in python. However when i ran them and compared the times, gnome sort which is O(n^2) was faster than quicksort which has a O(nlogn) i believe. Question: Can someone please explain to me why my gnome sort algorithm is faster than my quick and merge sort algorithms and ways to optimize my them so that they sort faster than the less efficient algorithms. Thank you. (P.S. if you've never heard of gnome sort here's a link that will help Wikipedia Gnome Sort Algorithm)
# Sorting lib
# author: Aniekan Umoren
# course: LEAP EngTech
# date started: 2016-08-09
# last modified: 2016-08-16
import time
from random import randrange
def bubble_sort(arr):
k = 0
n = len(arr)
for numSweeps in range(n-1):
for i in range(n-k-1):
if arr[i] > arr[i+1]:
temp = arr[i]
arr[i] = arr[i+1]
arr[i+1] = temp
k += 1
return arr
def selection_sort(arr):
n = len(arr)
k = 0
for numSweeps in range(n-1):
minimum = arr[k]
for i in range(k,n):
if arr[i] < minimum:
temp = arr[k]
arr[k] = arr[i]
arr[i] = temp
minimum = arr[k]
k += 1
return arr
def gnome_sort(arr):
pos = 0
while pos < len(arr):
if pos == 0 or arr[pos] >= arr[pos-1]:
pos += 1
else:
temp = arr[pos]
arr[pos] = arr[pos-1]
arr[pos-1] = temp
pos -= 1
return arr
def insertion_sort(arr):
for i in range(len(arr)):
x = arr[i]
j = i -1
while j >= 0 and arr[j] > x:
arr[j+1] = arr[j]
j -= 1
arr[j+1] = x
return arr
def merge(arr1,arr2):
arr3 = []
size1 = len(arr1); size2 = len(arr2)
i1 = 0; i2 = 0
while i1 < size1 or i2 < size2: #both list aren't empty
if i1 == size1:
arr3.append(arr2[i2])
i2 += 1
elif i2 == size2:
arr3.append(arr1[i1])
i1 += 1
elif arr1[i1] <= arr2[i2]:
arr3.append(arr1[i1])
i1 += 1
elif arr2[i2] < arr1[i1]:
arr3.append(arr2[i2])
i2 += 1
return arr3
def merge_sort(Arr, start, end):
if start < end:
# size = (start + end + 1)
mid = (start+end)//2
arr1 = merge_sort(Arr,start, mid)
arr2 = merge_sort(Arr, mid+1, end)
Arr[start:end+1] = merge(arr1, arr2)
return Arr[start:end+1]
def partition(Arr, start, end):
rand = start + randrange(end-start)
temp = Arr[start]
Arr[start] = Arr[rand]
Arr[rand] = temp
i = start + 1
for j in range(start+1, end+1):
if Arr[j] < Arr[start]:
temp = Arr[i]
Arr[i] = Arr[j]
Arr[j] = temp
i += 1
temp = Arr[start]
Arr[start] = Arr[i-1]
Arr[i-1] = temp
return (Arr,i-1)
def quick_sort(Arr, start, end):
if start < end:
part_result = partition(Arr, start, end)
Arr = part_result[0]
piv_pos = part_result[1]
quick_sort(Arr, start, piv_pos-1)
quick_sort(Arr, piv_pos+1, end)
if end == len(Arr)-1:
return Arr
def main():
start_time = time.time()
li1 = [3, 1234, 123, 214, 21, 124, 125, 213,
34, 354, 2345,62, 34, 623, 34, 34, 53465,
346, 346434, 537373, 5347,73, 234, 62, 36,
27, 247, 4742, 47472, 24, 742, 57, 24, 7245, 24]
li2 = [3, 21, 24, 24, 27, 34, 34, 34, 34, 36,
57, 62, 62, 73, 123, 124, 125, 213, 214,
234, 247, 346, 354, 623, 724, 742, 1234,
2345, 4742, 5347, 7245, 47472, 53465, 346434, 537373]
li3 = sorted(li2, reverse = True)
li4 = [3,5,26,42,2,6]
for i in range(10000):
result = bubble_sort(li1)
print("BUBBLE SORT: %s seconds" % (time.time() - start_time))
start_time = time.time()
for i in range(10000):
result = gnome_sort(li1)
print("GNOME SORT: %s seconds" % (time.time() - start_time)) # why is gnome sort sooo fast if it has a O(n**2)
start_time = time.time()
for i in range(10000):
result = selection_sort(li1)
print("SELECTION SORT: %s seconds" % (time.time() - start_time))
start_time = time.time()
for i in range(10000):
result = insertion_sort(li1)
print("INSERTION SORT: %s seconds" % (time.time() - start_time))
size = len(li1)
start_time = time.time()
for i in range(10000):
result = merge_sort(li1, 0, size-1)
print("MERGE SORT: %s seconds" % (time.time() - start_time))
size = len(li1)
start_time = time.time()
for i in range(10000):
result = quick_sort(li1, 0, size-1)
print("QUICK SORT: %s seconds" % (time.time() - start_time))
start_time = time.time()
for i in range(10000):
result = sorted(li1)
print("TIM SORT: %s seconds" % (time.time() - start_time))
main()
Your sorts all mutate the input instead of returning a new list, so after the first sort, the input is sorted and stays sorted. Gnome sort is O(n) on already-sorted input.

My wxpython program suddenly doesn't work for no reason

I made a program for school few weeks ego.. i have finished it yet.
for some reason, when i am trying to run the program now, it doesn't work, although it worked well in the last time a tried.
I am sure that i didn't change anything in the code, but there is always a chance that something has changed and i didn't payed attention..
I need to pass it to my teacher in these days and i have no idea what is wrong with the program.
I will glad to get some help here..
here is the code:
import wx
import winsound
import wx.grid as gridlib
from random import randint
OPTIONS = [1, 2, 3, 4, 5, 6, 7, 8, 9, "DEL", 0, "SEND"]
# these are the events' IDs sent to a function when you click a button.
# the OPTIONS_ID is in the same order of OPTIONS.
OPTIONS_ID = [-31984,-31983,-31982,-31981,-31980,-31979, -31978, -31977, -31976, -31975, -31974, -31973, -31985] # the built in wxpython IDs for the buttons
GAME_POSITION = (400, 100)
GAME_SIZE = [900, 600]
def RandomNum():
count = 5
while count > 4:
num = randint(1000, 9999)
digits = str(num)
count = 0
for digit in digits:
for digit2 in digits:
if digit == digit2:
count = count + 1
return digits
class Frame(wx.Frame): # class for all the frames in our game.
def __init__(self, parent, id, title, pos, size):
wx.Frame.__init__(self, parent, id, title, pos, size)
self.panel = wx.Panel(self)
self.fdf = wx.TextCtrl(self.panel, size=(275, 75), pos=(520, 20))
self.count = 0
self.turnsCounter = 0
self.numbers = RandomNum()
self.bulls = 0
self.cows = 0
self.counter_of_turns = 0
self.check = False
self.grid = gridlib.Grid(self.panel, pos = (85, 150), size=(323, 212))
self.grid.CreateGrid(10, 3)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.panel, 1, wx.EXPAND)
sizer.Add(self.grid, 1, wx.EXPAND)
self.panel.SetSizer(sizer)
for i in range(10):
for j in range(4):
self.grid.SetReadOnly(i, j)
self.grid.SetColLabelValue(0, "guess")
self.grid.SetColLabelValue(1, "cows")
self.grid.SetColLabelValue(2, "bulls")
def message_dialog(self, message, caption, style=wx.YES_NO, position=GAME_POSITION):
if message != "": # making sure not to execute a message if its empety
message = wx.MessageDialog(None, message, caption, style, position)
answer = message.ShowModal()
if answer == wx.ID_YES:
self.reset()
else:
self.Destroy()
else:
return -1
# this function creates a textbox at a specific position with a specific size.
def write(self, panel, txt, pos, size=20, font_family=wx.SWISS, font_style = wx.NORMAL,font_weight = wx.BOLD, underline = False, color=wx.WHITE):
# create a textbox at a specific position with a specific size.
your_txt = wx.StaticText(panel, -1, txt, pos)
your_txt.SetFont(wx.Font(size,font_family,font_style,font_weight,underline))
your_txt.SetForegroundColour(color)
# same as above, just for a button.
def create_button(self, panel, txt, position, width, height, color, disable):
Size = wx.Size(width, height)
self.button = wx.Button(panel, -1, txt, position, Size)
self.button.SetBackgroundColour(color)
self.border = wx.BoxSizer(wx.VERTICAL)
self.border.Add(self.button)
self.Bind(wx.EVT_BUTTON, lambda evt: self.OnButton(evt), self.button)
if disable == True:
self.button.Disable()
def count_bulls(self, txtctrl, seria):
for i in range(4):
if seria[i] == txtctrl[i]:
self.bulls += 1
replacement = self.bulls
self.bulls = 0
return replacement
def count_cows(self, txtctrl, seria):
for i in range(4):
if seria[i] != txtctrl[i] and seria[i] in txtctrl:
self.cows += 1
replacement = self.cows
self.cows = 0
return replacement
def reset(self):
self.fdf.Clear()
self.grid.ClearGrid()
self.count = 0
self.turnsCounter = 0
self.numbers = RandomNum()
self.bulls = 0
self.cows = 0
self.counter_of_turns = 0
self.check = False
for child in self.panel.GetChildren():
if child.GetLabel() != "SEND":
child.Enable()
else:
child.Disable()
if self.count == 0:
if child.GetLabel() == "DEL" or child.GetLabel() == "0":
child.Disable()
def OnButton(self, event):
print repr(event.Id) + ","
print self.numbers
if event.Id in OPTIONS_ID: # if indeed an option button was pressed
exited = -1 # exited is 5100 if the user exited his dialog box
# assigning the events to the button.
for i in range(13):
if event.Id != -31985 and event.Id != -31975 and event.Id != -31974 and event.Id != -31973 and event.Id == OPTIONS_ID[i]:
self.fdf.AppendText(str(OPTIONS[i]))
self.count += 1
if event.Id == -31974:
self.fdf.AppendText(str(OPTIONS[10]))
self.count += 1
if event.Id == -31985:
self.reset()
if event.Id == -31973:
self.counter_of_turns += 1
print self.numbers
print self.fdf.GetValue()
cows = self.count_cows(self.fdf.GetValue(), self.numbers)
bulls = self.count_bulls(self.fdf.GetValue(), self.numbers)
self.grid.SetCellValue(self.turnsCounter,0, self.fdf.GetValue())
self.grid.SetCellValue(self.turnsCounter, 1, str(cows))
self.grid.SetCellValue(self.turnsCounter, 2, str(bulls))
self.fdf.Clear()
self.count = 0
if self.turnsCounter < 9:
self.turnsCounter += 1
if bulls == 4:
self.check = True
winsound.PlaySound('The_Power_-_Snap_1_.wav', winsound.SND_ASYNC | winsound.SND_LOOP)
self.message_dialog("Well done! you won this game..\n You won the game in %s turns .. \n Play again ? " % self.counter_of_turns , "You won!")
winsound.PlaySound(None, 0)
if event.Id == -31975:
if self.count > 0:
self.count -= 1
self.fdf.Remove(self.fdf.GetLastPosition()-1, self.fdf.GetLastPosition())
if self.count == 4:
for child in self.panel.GetChildren():
if isinstance(child, wx.Button):
try:
int(child.GetLabel())
except ValueError:
if child.GetLabel() == "SEND":
child.Enable()
else:
child.Disable()
elif self.check == False:
for child in self.panel.GetChildren():
if child.GetLabel() != "SEND":
child.Enable()
else:
child.Disable()
if self.count == 0:
if child.GetLabel() == "DEL" or child.GetLabel() == "0":
child.Disable()
#for child in self.panel.GetChildren():
#if isinstance(child, wx.Button):
#if child.GetLabel() in self.fdf.GetValue():
#child.Disable()
if self.counter_of_turns == 10 and self.check == False:
self.message_dialog("YOU LOST :( \n THE NUMBERS WERE %s \n PLAY AGAIN ?" % self.numbers,"Bad news ..")
class Game(wx.App):
def OnInit(self): # upon game opening
# I would like the options window to be the first window's parent
# so I will first set up our options window:
window = Frame(None, -1, "Good Luck!", GAME_POSITION, GAME_SIZE)
first_panel = window.panel
window.SetBackgroundColour(wx.BLACK)
window.write(first_panel, "BULLS AND COWS!", (50, 50), size=(35))
countX = 500
countY = 100
window.create_button(first_panel,"restart!", (50, 400), 100, 100, wx.WHITE, False)
for option in OPTIONS:
if str(option) == "SEND" or str(option) == "DEL":
window.create_button(first_panel,str(option), (countX, countY), 100, 100, wx.GREEN, True)
elif str(option) == "0":
window.create_button(first_panel,str(option), (countX, countY), 100, 100, wx.WHITE, True)
else:
window.create_button(first_panel,str(option), (countX, countY), 100, 100, wx.WHITE, False)
countX += 110
if str(option) == "3" or str(option) == "6" or str(option) == "9":
countY += 110
countX = 500
window.Show(True)
return True
def main():
MasterMind = Game()
MasterMind.MainLoop()
if __name__ == '__main__':
main()
PLEASE NOTE:
I upgraded my windows to windows 10 few days ego, it means that it doesn't work since the upgrade if it means something. (sorry if the grammer of my english not so well, it is not my native language..).
In my computer works fine (Python 2.7.11 and windows 7). If you import this class in another file, make sure if you call the main function.

Resources